Javascript Array toString()

Introduction

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

arr.toString()
var prices = [500, 1234, 12345678]; 
const a = prices.toString();
console.log(a);// www .ja  v a 2s  .co m

Convert an array to a string:

var languages = ["CSS", "HTML", "Java", "Javascript"];
var x = languages.toString();
console.log(x);/*from  www . jav a2 s  .c  o m*/

The difference between toString() and toLocaleString()

 Code:
 let person1 = {/* w  ww.  j a  v  a2  s  .c om*/
     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