Javascript Array toLocaleString()

The JavaScript Array toLocaleString() method returns a string representing the elements of the array.

The syntax of the toLocaleString() method is:

arr.toLocaleString(locales, options)

Here, arr is an array.


toLocaleString() Parameters

The toLocaleString() method takes in:

  • locales (optional) - A string with a BCP 47 language tag, or an array of such strings. To learn more, visit Intl - JavaScript.
  • options (optional) - An object with configuration properties.

Return value from toLocaleString()

  • Returns a string representing the elements of the array.

Note: This method converts each array elements to Strings using their toLocaleString methods and separates them by a comma.


Example: Using toLocaleString() method

let array = [1, "JavaScript", new Date()];

let string = array.toLocaleString();
console.log(string); // 1,JavaScript,7/29/2020, 1:53:29 PM

let prices = [689, 100, 4577, 56];

// Using locales and options
// using Nepali Ruppes currency string format
let string1 = prices.toLocaleString("ne-NP", {
  style: "currency",
  currency: "NPR",
});

console.log(string1); // NPR 689.00,NPR 100.00,NPR 4,577.00,NPR 56.00

Output

1,JavaScript,7/29/2020, 1:57:35 PM
NPR 689.00,NPR 100.00,NPR 4,577.00,NPR 56.00

Recommended Reading: Javascript Array toString()