JavaScript(JS) array method - tolocalestring

www.igif‮oc.aedit‬m

The toLocaleString() method in JavaScript is used to return a string representing the elements of an array. The elements are converted to strings using the specified locale and then concatenated into a single string using a separator.

The toLocaleString() method takes two optional arguments, which are locale and options. The locale argument is a string that specifies the locale to use when converting the elements to strings. The options argument is an object that allows you to specify additional options for the conversion.

Here is an example of using the toLocaleString() method:

const numbers = [1000, 2000, 3000];
const string = numbers.toLocaleString('en-US', { style: 'currency', currency: 'USD' });
console.log(string); // $1,000.00, $2,000.00, $3,000.00

In the example above, the toLocaleString() method is used to convert the numbers array to a string representing currency values in US dollars. The resulting string is $1,000.00, $2,000.00, $3,000.00.

Note that the toLocaleString() method does not modify the original array. Instead, it returns a new string that represents the array elements.