The Array.toLocaleString() method returns a string representing the elements of the array in a particular locale.
Example
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
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
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.
Recommended Reading: Javascript Array toString()