.toLocaleString()

Returns the string representation of an array based on the results of converting each element to a string according to the type of element and the passed localization parameters.

Time to read: less than 5 min

Briefly

The method toLocaleString() returns the representation of an array as a string consisting of the results of converting each element of the array to a string according to the type of element and the passed localization parameters. The value separator is the character defined according to the localization parameters of the runtime environment (, or another used for separating elements in a list).

For array elements that are null or undefined, an empty string is returned.

Example

Let's get a string from the array considering the localization de-DE (German language and regional standards for numbers and dates in Germany):

        
          
          const array = [null, 50, 15000, new Date('2007-08-10')]const arrayStr = array.toLocaleString('de-DE')console.log(arrayStr)// 50,15.000,10.8.2007, 03:00:00
          const array = [null, 50, 15000, new Date('2007-08-10')]
const arrayStr = array.toLocaleString('de-DE')

console.log(arrayStr)
// 50,15.000,10.8.2007, 03:00:00

        
        
          
        
      

Let's get a string from the same array considering the localization en-US (English language and regional standards for numbers and dates in the USA) and specifying the time zone:

        
          
          const array = [null, 50, 15000, new Date('2007-08-10')]const arrayStr = array.toLocaleString(  'en-US', {timeZone: 'Europe/Amsterdam'})console.log(arrayStr)// 50,15,000,8/10/2007, 2:00:00 AM
          const array = [null, 50, 15000, new Date('2007-08-10')]
const arrayStr = array.toLocaleString(
  'en-US', {timeZone: 'Europe/Amsterdam'}
)
console.log(arrayStr)
// 50,15,000,8/10/2007, 2:00:00 AM

        
        
          
        
      

How it works

Array.toLocaleString() takes two optional arguments:

  • locales — a string that defines the locale (information about the user's language and regional settings) or an array of such strings;
  • options — a configuration object used when calling the toLocaleString() method on each element of the array.

Array.toLocaleString() returns a string consisting of the results of invoking the toLocaleString() method for all elements of the array, separated by a comma or another separator character.

If the method is called without arguments, the localization parameters set in the runtime environment will be used.

How to understand it

The toLocaleString() method is intended to represent data according to the specified localization.

Localization is needed for numbers, dates, and currency units, as different regional standards use various formats for presenting this data. For example, the number 1000 can be represented as a string in different ways:

  • 1,000en-US;
  • 1 000ru-RU;
  • 1.000de-DE.

The toLocaleString() method is defined in the prototype Object.prototype and is inherited by all built-in objects.

In addition to Object and Array, built-in objects have their own implementations of the toLocaleString() method:

  • Number;
  • Date;
  • BigInt;
  • TypedArray.

Traversing the array, the toLocaleString() method invokes toLocaleString() for each element, according to its type (if the value is not null or undefined) and combines the result into a string, using the established separator (usually a comma).

If an array element is a primitive type, the value is wrapped in the corresponding wrapper object to apply the toLocaleString() method to it.

All primitive type values except null and undefined have a corresponding wrapper object. For example: true — Boolean, 175 — Number, 'Aquarium' — String.

The array method toLocaleString() is a convenient way to obtain a string from an array of data considering the localization parameters.

Let's get a string of prices in euros from an array of numbers:

        
          
          const numbers = [150, 20, 800, 0]const pricesStr = numbers.toLocaleString(  'de-DE', {style: 'currency', currency: "EUR"})console.log(pricesStr)// 150,00 €,20,00 €,800,00 €,0,00 €
          const numbers = [150, 20, 800, 0]
const pricesStr = numbers.toLocaleString(
  'de-DE', {style: 'currency', currency: "EUR"}
)
console.log(pricesStr)
// 150,00 €,20,00 €,800,00 €,0,00 €