Skip to content
Related Articles

Related Articles

Improve Article

JavaScript Object.prototype.toLocaleString() Method

  • Last Updated : 20 May, 2021

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:

Javascript




// User inputs.
var name = [ "sahil", "zain", "deepanshu" ];
var number1 = 3.45;
var number2 = [ 23, 34, 54 ];
  
var arr = [ name, number1, number2 ];
    
// Applying array.toLocaleString function
var string = arr.toLocaleString();
    
// Printing string.
console.log(string);

Output:

"sahil, zain, deepanshu, 3.45, 23, 34, 54"

2. BigInt to BigInt.prototype.toLocaleString(): It returns a string representation of this BigInt object.

Example:

Javascript




var Big = 45334n;
console.log(Big.toLocaleString());
  
Big =78753456789123456789n;
console.log(Big.toLocaleString('de-DE'));

Output:



"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:

Javascript




var d = new Date(Date.UTC(2020, 9, 26, 7, 0, 0));
var result = d.toLocaleString();
   console.log("Date and Time of apocalypse: "+ result);

Output:

Date and Time of apocalypse: 26/10/2020, 12:30:00

4. Number to Number.prototype.toLocaleString(): It returns a string representation of this number.

Example:

Javascript




// Declaring an variable 
  var a = new Number(159900);
    
  // Creating an dictionary like object and
  // include currency and style
  var myObj = {
    style: "currency",
    currency: "EUR"
  }
    
  console.log(a.toLocaleString("en-GB", myObj));

Output:

€159,900.00

5. TypedArray to TypedArray.prototype.toLocaleString() It returns a string which represents the element of the typedArray.

Example:

Javascript




var 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"

Supproted Browsers:

  • Google Chrome
  • Internet Explorer
  • Firefox
  • Apple Safari
  • Opera



My Personal Notes arrow_drop_up
Recommended Articles
Page :