In this article, you will learn about the javascript Array built-in method Array.prototype.join(). How does this method work in javascript?
This Array.prototype.join() method joins the elements of an array and returns a new string separated by commas or a specified separator string. If the length of the array is 0, then it will return the empty string.
Note: If the array has only one item, then it will return the string without using the separator.
If an element is undefined, or null in an array, it is converted to an empty string instead of the string "null" or "undefined"
array.join(seperator);
This method takes 1 parameter as given below:
The join() method does not change the original array.
Here are some examples of Array.prototype.join() method:
//Here some examples of Array.join() Method
const arr = ['Java', 'C#', 'Python', 'SQL Server'];
console.log(arr.join());
// Output => "Java,C#,Python,SQL Server"
console.log(arr.join(', '));
// Output => "Java, C#, Python, SQL Server"
console.log(arr.join(' + '));
// Output => "Java + C# + Python + SQL Server"
console.log(arr.join(''));
// Output => "JavaC#PythonSQL Server"
//Using join() on sparse arrays
const arr1 = ['Java', , 'C#', 'Python', undefined, 'SQL Server', null];
console.log(arr1.join());
// Output => "Java,,C#,Python,,SQL Server,"
I hope this article will help you to understand the javascript Array built-in method Array.prototype.join().
Share your valuable feedback, please post your comment at the bottom of this article. Thank you!
Comments