Javascript Array join()

Introduction

Javascript join() method returns a string.

It appends all array elements to a string using commas or a specified separator.

If the array has only one item, that item will be returned without using the separator.

arr.join([separator])
  • separator - Optional, If omitted, the array elements are separated with a comma (",").

If separator is an empty string, all elements are joined without any characters.

If arr.length is 0, the empty string is returned.

If an element is undefined, null or an empty array [], it is converted to an empty string.

Convert the elements of an array into a string:

var languages = ["CSS", "HTML", "Java", "Javascript"];
console.log(languages.join());

Try using a different separator:

var languages = ["CSS", "HTML", "Java", "Javascript"];
console.log(languages.join(" and "));

More example

var a = ['CSS', 'HTML', 'Javascript'];
let s = a.join();      
console.log(s);/*  w w w. j ava 2 s . c o m*/
s = a.join(', ');  
console.log(s);
s = a.join(' + '); 
console.log(s);
s = a.join('');    
console.log(s);
s = a.join(' ');    
console.log(s);

More example:

let colors = ["red", "green", "blue"]; 
console.log(colors.join(","));    // red,green,blue 
console.log(colors.join("||"));   // red||green||blue 



PreviousNext

Related