JavaScript Object toLocaleString() Method
Last Updated :
20 Jul, 2023
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():
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
let name = [ "sahil" , "zain" , "deepanshu" ];
let number1 = 3.45;
let number2 = [23, 34, 54];
let arr = [name, number1, number2];
let string = arr.toLocaleString();
console.log(string);
|
Output
sahil,zain,deepanshu,3.45,23,34,54
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' ));
|
Output
45,334
78.753.456.789.123.456.789
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);
|
Output
Date and Time of apocalypse: 10/26/2020, 7:00:00 AM
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
let a = new Number(159900);
let myObj = {
style: "currency" ,
currency: "EUR"
}
console.log(a.toLocaleString( "en-GB" , myObj));
|
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' }));
|
Output
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
Similar Reads
JavaScript Constructor Method
A constructor in JavaScript is a special function used to create and initialize objects. It sets up object properties and is typically invoked using the new keyword. Constructors allow for the creation of multiple instances with similar properties and methods. In JavaScript, constructors can be defi
8 min read
JavaScript Object assign() Method
The Object.assign() method is used to copy the values and properties from one or more source objects to a target object. It invokes getters and setters since it uses both [[Get]] on the source and [[Set]] on the target. Syntax:Object.assign(target, ...sources);Parameters:target: It is the target obj
4 min read
JavaScript Object create() Method
JavaScript object.create() method is used to create a new object with the specified prototype object and properties. Object.create() method returns a new object with the specified prototype object and properties. Syntax:Object.create(prototype[, propertiesObject])Parameters:prototype: It is the prot
3 min read
JavaScript Object defineProperty() Method
The Object.defineProperty() method in JavaScript is a Standard built-in object which defines a new property directly on an object or it can also modify the existing property of an object and return the object. Syntax:Object.defineProperty(obj, prop, descriptor)Parameters:This method accepts three pa
3 min read
JavaScript Object defineProperties() Method
The Object.defineProperties() method in JavaScript is a standard built-in Object that defines a new or modifies existing properties directly on an object and it returns the object. Syntax:Object.defineProperties(obj, props) Parameters:Obj: This parameter holds the object on which the properties are
2 min read
JavaScript Object entries() Method
The Object.entries() method in JavaScript is used to retrieve an array of an object's enumerable property [key, value] pairs. This method is particularly useful for transforming and iterating over objects in situations where array-like manipulation is needed. Syntax:Object.entries(obj);Parameters:ob
4 min read
JavaScript Object freeze() Method
The Object.freeze() method is used to freeze an object. Freezing an object does not allow new properties to be added to the object and prevents removing or altering the existing properties. Object.freeze() preserves the enumerability, Configurability, writability, and prototype of the object. It ret
3 min read
JavaScript Object getOwnPropertyDescriptor() Method
The Object.getOwnPropertyDescriptor() method in JavaScript is a standard built-in object that enables the full information on a property to be accessed and returns a property descriptor for the own property of a given object. Syntax: Object.getOwnPropertyDescriptor( obj, prop ) Parameters: This meth
2 min read
JavaScript Object getOwnPropertyNames() Method
The Object.getOwnPropertyNames() method in JavaScript is a standard built-in object which returns all properties that are present in a given object except for those symbol-based non-enumerable properties. Syntax:Object.getOwnPropertyNames(obj)Parameters:This method accepts a single parameter as ment
3 min read
JavaScript Object getOwnPropertySymbols() Method
The Object.getOwnPropertySymbols() method in JavaScript is a standard built-in object which returns an array of all symbol properties that are present in a given object. An empty array is returned until symbol properties are set on the object. Syntax: Object.getOwnPropertySymbols(obj) Parameters: ob
2 min read