;

Javascript Array - toString() Method


Tutorialsrack 05/11/2022 Jquery Javascript

In this article, you will learn about the Javascript Array built-in method Array.prototype.toString().  How does this method work in javascript? 

Array.prototype.toString()

The Array.prototype.toString() method is used to return a string containing all the elements of a specified array. This method separates them by a comma. 

Syntax
array.toString();

This method does not take any parameters. And the toString() method does not change the original array.

Here are some examples of Array.prototype.toString() method:

Examples
const language = ["Java", "C#", "Go", "JavaScript", "Python"];

console.log(language.toString());
// Output => "Java,C#,Go,JavaScript,Python"


const prices = ['₹7', 500, 8123, 12];
console.log(prices.toString());
//Output => "₹7,₹500.00,₹8,123.00,₹12.00"


//Using toString() on sparse arrays
console.log([1, , 3].toString()); 
//Output => '1,,3'

I hope this article will help you to understand the javascript Array built-in method Array.prototype.toString()

Share your valuable feedback, please post your comment at the bottom of this article. Thank you!


Related Posts



Comments

Recent Posts
Tags