Javascript Array toLocaleString()

Introduction

Javascript array toLocaleString() method returns a string representing the array.

The elements are converted to Strings using their toLocaleString methods.

These Strings are separated by a locale-specific String, such as a comma.

arr.toLocaleString([locales[, options]]);
  • locales - Optional, a string with a language tag, or an array of such strings.
  • options - Optional, an object with configuration properties.
var prices = [500, 1234, 12345678]; 
const a = prices.toLocaleString('ja-JP', { style: 'currency', currency: 'JPY' });
console.log(a);/*from w w w.j av  a 2s . c  o m*/

The difference between toString() and toLocaleString()

 Code:
 let person1 = {/* ww w.j  a  va  2 s.c o  m*/
     toLocaleString() {
         return "Oracle";
     },
             
     toString() {
         return "HTML";
     }
 };
             
 let person2 = {
     toLocaleString() {
         return "Server";
     },
             
     toString() {
         return "Javascript";
     }
 };
             
 let people = [person1, person2];
 console.log(people);             // HTML,Javascript 
 console.log(people.toString());  // HTML,Javascript 
 console.log(people.toLocaleString()); // Oracle,Server 



PreviousNext

Related