JavaScript Object toLocaleString() Method
The Object.prototype.toLocaleString() method returns a local specific string representation of this object using the locale of the environment. Derived objects like an array, number, date, typedarray, and BigInt can override this method.
Syntax:
object.toLocaleString()
Return Value: It returns a string representation of this object.
Objects overriding toLocaleString():
1. Array to Array.prototype.toLocaleString()
It returns a string representation of this array object.
Example: The code creates an array arr containing name, number1, and number2, then converts it to a string using toLocaleString(), and prints the string.
Javascript
// User inputs. let name = [ "sahil" , "zain" , "deepanshu" ]; let number1 = 3.45; let number2 = [23, 34, 54]; let arr = [name, number1, number2]; // Applying array.toLocaleString function let string = arr.toLocaleString(); // Printing string. console.log(string); |
sahil,zain,deepanshu,3.45,23,34,54
2. BigInt to BigInt.prototype.toLocaleString()
It returns a string representation of this BigInt object.
Example: The code creates a BigInt variable Big, converts it to a localized string, and prints it twice with different locale settings.
Javascript
let Big = 45334n; console.log(Big.toLocaleString()); Big = 78753456789123456789n; console.log(Big.toLocaleString( 'de-DE' )); |
45,334 78.753.456.789.123.456.789
3. Date to Date.prototype.toLocaleString()
It returns a string representation of this date object.
Example: The code creates a Date object representing a specific date and time, converts it to a localized string, and logs the result.
Javascript
let d = new Date(Date.UTC(2020, 9, 26, 7, 0, 0)); let result = d.toLocaleString(); console.log( "Date and Time of apocalypse: " + result); |
Date and Time of apocalypse: 10/26/2020, 7:00:00 AM
4. Number to Number.prototype.toLocaleString()
It returns a string representation of this number.
Example: The code converts the number to a localized string with British English formatting and currency style (EUR).
Javascript
// Declaring an variable let a = new Number(159900); // Creating an dictionary like object and // include currency and style let myObj = { style: "currency" , currency: "EUR" } console.log(a.toLocaleString( "en-GB" , myObj)); |
€159,900.00
5. TypedArray to TypedArray.prototype.toLocaleString()
It returns a string which reprethatents the element of the typedArray.
Example: The code converts the elements of the Uint32Array to localized strings in default, en-US, and Hindi with custom currency formatting (HIR).
Javascript
let geek = new Uint32Array([100, 897, 123, 132, 22]); console.log(geek.toLocaleString()); console.log(geek.toLocaleString( 'en-US' )); console.log(geek.toLocaleString( 'hi' , { style: 'currency' , currency: 'HIR' })); |
100,897,123,132,22 100,897,123,132,22 HIR 100.00,HIR 897.00,HIR 123.00,HIR 132.00,HIR 22.00
Supported Browsers:
- Google Chrome 1 and above
- Internet Explorer 5.5 and above
- Firefox 1 and above
- Apple Safari 1 and above
- Opera 4 and above
- Edge 12 and above