The Object.toLocaleString() method returns a language-sensitive string representation of the object.
Example
toLocaleString() Syntax
The syntax of the toLocaleString() method is:
obj.toLocaleString(Locales, options)
Here, obj is the object whose language-specific string representation is required.
toLocaleString() Parameters
The toLocaleString() method does not take in any parameters by default. However, it can have optional parameters:
Locales- specifies which language format to use, such asen-US(American English),en-IN(Indian English) and so onoptions- object that helps set some properties to customize the string representation
Note: The Locales and options arguments customize the behavior of the function to specify the desired formatting conventions based on the language or region.
toLocaleString() Return Value
The toLocaleString() method returns a string representing the object.
Some objects overriding Object.toLocaleString():
Example 1: JavaScript toLocaleString() With Array
In the above example, the toLocaleString() converts the array arr into a formatted string having currency format for the French Locale and using Euros as the currency symbol.
In the above code, fr represents the French locale and EUR represents the Euro currency.
Example 2: toLocaleString() Method With Number
Example 3: toLocaleString() Method With Date
Note: The Object.toLocaleString() method usually returns the result of toString(). This method is meant to be overridden by derived objects for locale-specific purposes, even though all may not use it.
Recommended Reading: