JavaScript Array.toLocaleString()

The Array.toLocaleString() method returns a string representing the elements of the array in a particular locale.

Example

let array1 = ["Nepal", 1];

// returns string representation of array let stringFromArray = array1.toLocaleString();
console.log(stringFromArray); // Output: // Nepal,1

toLocaleString() Syntax

The syntax of the toLocaleString() method is:

arr.toLocaleString(locales, options)

Here, arr is an array.


toLocaleString() Parameters

The toLocaleString() method can take two parameters:

  • locales (optional) - A convention or formatting based on particular geography.
  • options (optional) - An object with configuration properties.

toLocaleString() Return Value

  • Returns a string representing the elements of the array.

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


Example 1: Using toLocaleString() Method

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

// returns string representation of array let stringFromArray = array1.toLocaleString();
console.log(stringFromArray);

Output

1,JavaScript,5/9/2022, 2:11:22 PM

In the above example, we have used the toLocaleString() method to convert array1 to a string representing its elements.

We have created array1 with three elements: 1, 'JavaScript' and new Date() where the third element creates a Date object.

array1.toLocaleString() returns the string representation of these elements i.e. 1,JavaScript,5/9/2022, 2:11:22 PM which is separated by locale-specific string like comma.


Example 2: toLocaleString() Method with Parameters

// defining an array 
let prices = [689, 100, 4577, 56];

// passing locales and options: 
// using United States Dollar currency string format
let resultingString = prices.toLocaleString("en-US", { style: "currency", currency: "USD", });
console.log(resultingString);

Output

$689.00,$100.00,$4,577.00,$56.00

Here we have passed locales and options parameters in the toLocaleString() method that specifies the United States Dollar currency string format.

The method converts prices to the string based on the passed format.


Also Read:

Did you find this article helpful?