The toLocaleString() method of the Array class in JavaScript returns a localized string representing the calling array and its elements. This method overrides the Object.prototype.toLocaleString() method.
There are 3 variations for the syntax of Array.toLocaleString() method. They are:
toLocaleString()
This method returns a localized string representing the calling array and its elements, using the default locale and options.
Returns value of type String
.
toLocaleString(locales)
Parameters
Parameter | Optional/Required | Description |
---|---|---|
locales | optional | A string with a BCP 47 language tag, or an array of such strings. This parameter is used to specify the locale or locales. |
This method returns a localized string representing the calling array and its elements, using the specified locales.
Returns value of type String
.
toLocaleString(locales, options)
Parameters
Parameter | Optional/Required | Description |
---|---|---|
locales | optional | A string with a BCP 47 language tag, or an array of such strings. This parameter is used to specify the locale or locales. |
options | optional | An object with configuration properties. This parameter is used to specify options for the locale-specific formatting. |
This method returns a localized string representing the calling array and its elements, using the specified locales and options.
Returns value of type String
.
In JavaScript, we can use the toLocaleString() method to get a localized string representation of an array using the default locale and options.
For example,
const arr = [1234.56, 'test', new Date()];
const localizedString = arr.toLocaleString();
console.log(localizedString);
1,234.56,test,5/31/2024, 12:00:00 AM
We can use the toLocaleString() method to get a localized string representation of an array using a specified locale.
For example,
const arr = [1234.56, 'test', new Date()];
const localizedString = arr.toLocaleString('de-DE');
console.log(localizedString);
1.234,56,test,31.5.2024, 00:00:00
We can use the toLocaleString() method to get a localized string representation of an array using specified locales and options.
For example,
const arr = [1234.56, 'test', new Date()];
const options = { year: 'numeric', month: 'long', day: 'numeric' };
const localizedString = arr.toLocaleString('en-US', options);
console.log(localizedString);
1,234.56,test,May 31, 2024
In this JavaScript tutorial, we learned about toLocaleString() method of Array: the syntax and few working examples with output and detailed explanation for each example.