JavaScript Array join() method joins the elements of an array into a string using the specified separator. It does not change the original array.
Syntax
array.join(separator)
Parameters
separator(optional): It specifies what to put between the array elements. By default, a separator is a comma.
Return value
Returns array values, separated by the specified separator.
Visual Representation
Example 1: How to Use Array join() method
let apps = ["Insta", "Fb", "Snap", "x", "Youtube"];
let apps_join = apps.join();
console.log(app_join);
Output
Insta,Fb,Snap,x,Youtube
In the above example, the array apps convert to a string where each element of the array is separated by a default specifier(comma).
Example 2: Joining an array using a custom separator
let apps = ["Insta", "Fb", "Snap", "x", "Youtube"];
let apps_join = apps.join('|');
console.log(apps_join);
letplus_join = apps.join(' + ');
console.log(letplus_join);
Output
Insta|Fb|Snap|x|Youtube
Insta + Fb + Snap + x + Youtube
Example 3: Using empty string
let apps = ["Insta", "Fb", "Snap", "x", "Youtube"];
let apps_join = apps.join('');
console.log(apps_join);
Output
InstaFbSnapxYoutube
Example 4: Using sparse arrays
This function treats empty slots like undefined ones and produces an extra separator.
let apps = ["Insta", , "Snap", , "Youtube"];
let apps_join = apps.join();
console.log(apps_join);
Output
Insta,,Snap,,Youtube
Example 5: Passing non-array objects
function aros(a, b, c) {
let data = Array.prototype.join.call(arguments);
console.log(data);
}
aros(21, 'acme', false);
Output
21,acme,false
Browser compatibility
- Google Chrome 1 and above
- Microsoft Edge 12 and above
- Mozilla Firefox 1 and above
- Safari 1 and above
- Opera 4 and above
Krunal Lathiya is a seasoned Computer Science expert with over eight years in the tech industry. He boasts deep knowledge in Data Science and Machine Learning. Versed in Python, JavaScript, PHP, R, and Golang. Skilled in frameworks like Angular and React and platforms such as Node.js. His expertise spans both front-end and back-end development. His proficiency in the Python language stands as a testament to his versatility and commitment to the craft.