JavaScript Array toLocaleString()
Syntax & Examples
Array.toLocaleString() method
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.
Syntax of Array.toLocaleString()
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
.
✐ Examples
1 Using toLocaleString() method to get a localized string representation of an array
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,
- We define an array variable arr with elements [1234.56, 'test', new Date()].
- We use the toLocaleString() method on arr to get a localized string representation of the array.
- The result is stored in the variable localizedString.
- We log localizedString to the console using console.log() method to see the localized string representation of the array.
JavaScript Program
const arr = [1234.56, 'test', new Date()];
const localizedString = arr.toLocaleString();
console.log(localizedString);
Output
1,234.56,test,5/31/2024, 12:00:00 AM
2 Using toLocaleString() method with a specified locale
We can use the toLocaleString() method to get a localized string representation of an array using a specified locale.
For example,
- We define an array variable arr with elements [1234.56, 'test', new Date()].
- We use the toLocaleString() method on arr with the locale 'de-DE' to get a localized string representation of the array in German.
- The result is stored in the variable localizedString.
- We log localizedString to the console using console.log() method to see the localized string representation of the array in German.
JavaScript Program
const arr = [1234.56, 'test', new Date()];
const localizedString = arr.toLocaleString('de-DE');
console.log(localizedString);
Output
1.234,56,test,31.5.2024, 00:00:00
3 Using toLocaleString() method with specified locales and options
We can use the toLocaleString() method to get a localized string representation of an array using specified locales and options.
For example,
- We define an array variable arr with elements [1234.56, 'test', new Date()].
- We define an options object options with properties { year: 'numeric', month: 'long', day: 'numeric' }.
- We use the toLocaleString() method on arr with the locale 'en-US' and options to get a localized string representation of the array in US English with the specified date format.
- The result is stored in the variable localizedString.
- We log localizedString to the console using console.log() method to see the localized string representation of the array in US English with the specified date format.
JavaScript Program
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);
Output
1,234.56,test,May 31, 2024
Summary
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.