Tutorialspoint.dev

JavaScript | array.toLocaleString() function

The array.toLocaleString() is an inbuilt function in JavaScript which is basically used to convert the element of the given array to string.
Syntax:

arr.toLocaleString();

Return values:
It return a string representing the elements of the array.
JavaScript Version:
ECMAScript 1
Examples:

Input: 
var name = "gfg";
var number = 123;
var A = [ name, number ];
Output:
> "gfg, 123"

Here as we see that in input there are two variables containing elements but in output these are converted into a string.
Let’s see JavaScript program on array.toLocaleString() function:

// taking inputs.
var name = "geeksforgeeks";
var number = 567;
  
// It will give current date and time.
var date = new Date();
  
// Here A is an array containing elements of above variables.
var A = [ name, number, date ];
  
// applying array.toLocaleString function.
var string = A.toLocaleString();
  
// printing string.
console.log(string);

Output:

> "geeksforgeeks, 567, 2/18/2018, 10:41:20 AM"

Application:
The array.toLocaleString() function in JavaScript is used whenever we need to convert the element of the given array to a string.
Let’s see JavaScript program on array.toLocaleString() function:

// taking inputs.
var name = [ "Ram", "Sheeta", "Geeta" ];
var number1 = 3.45;
var number2 = [ 23, 34, 54 ];
  
// Here A is an array containing elements of above variables.
var A = [ name, number1, number2 ];
  
// appling array.toLocaleString function.
var string = A.toLocaleString();
  
// printing string.
console.log(string);

Output:

> "Ram, Sheeta, Geeta, 3.45, 23, 34, 54"


This article is attributed to GeeksforGeeks.org

leave a comment

code

0 Comments

load comments

Subscribe to Our Newsletter