The Javascript array.toLocaleString() is an inbuilt function in JavaScript that is basically used to convert the elements of the given array to a string.
Syntax:
arr.toLocaleString(locales, options)
Return values: It returns a string representing the elements of the array.
JavaScript Version: ECMAScript 1
Examples: In this example, the variable number will be converted into a string using the array.toLocaleString() function.
Input: let name = "gfg"; let number = 123; let 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 the JavaScript program on array.toLocaleString() function:
Example: In this example, we will see the conversion of a number and a date into strings using array.toLocaleString() function.
JavaScript
// taking inputs. let name = "neveropen" ; let number = 567; // It will give current date and time. let date = new Date(); // Here A is an array containing elements of above variables. let A = [name, number, date]; // applying array.toLocaleString function. let string = A.toLocaleString(); // printing string. console.log(string); |
Output:
"neveropen, 567, 2/18/2018, 10:41:20 AM"
Application:
The array.toLocaleString() function in JavaScript is used whenever we need to convert the elements of the given array to a string.
Let’s see the JavaScript program on array.toLocaleString() function:
Example: In this example, we will see the conversion of an array and a decimal number into string values using array.toLocaleString() function.
JavaScript
// taking inputs. let name = [ "Ram" , "Sheeta" , "Geeta" ]; let number1 = 3.45; let number2 = [23, 34, 54]; // Here A is an array containing elements of above variables. let A = [name, number1, number2]; // applying array.toLocaleString function. let string = A.toLocaleString(); // printing string. console.log(string); |
Output:
"Ram, Sheeta, Geeta, 3.45, 23, 34, 54"
We have a complete list of Javascript Array methods, to check those please go through this Javascript Array Complete reference article.
Supported Browser:
- Google Chrome 1 and above
- Internet Explorer 5.5 and above
- Firefox 1 and above
- Opera 4 and above
- Safari 1 and above
We have a Cheat Sheet on Javascript where we covered all the important topics of Javascript to check those please go through Javascript Cheat Sheet-A Basic guide to JavaScript.