To convert array to string without commas in JavaScript, call the Array.join() method passing in an empty string as the parameter, like this – Array.join(”). This will concatenate all of the array elements into a single string with no commas between them.
Convert Array To String Without Commas In JavaScript
Array.prototype.join()
The Array.prototype.join() method joins all elements of an array into a string and returns this string.
Syntax
arr.join(separator)
Parameters
separator
Optional. Specifies a string to separate each element of the array. The separator is converted to a string if necessary. If omitted, the array elements are separated with a comma.
If separator is an empty string, all elements are joined without any characters in between them.
Return value
A string with all array elements joined.
Convert An Array To A String Without Commas In JavaScript
If you have an array of items and you want to convert it to a string without adding any commas between the items, you can use the Array.prototype.join() method. This method accepts an optional separator parameter that you can use to specify a different character (or characters) to use as the separator. If you omit the separator parameter, a comma will be used by default.
Here’s an example:
var arr = [‘a’, ‘b’, ‘c’];
var str = arr.join(");
console.log(str); // ‘abc’
You can use any character (or characters) you want as the separator. For example, if you wanted to use a dash (-) as the separator, you could do this:
var arr = [‘a’, ‘b’, ‘c’];
var str = arr.join(‘-‘);
console.log(str); // ‘a-b-c’
If you want to use space as a separator, you can do that too:
var arr = [‘a’, ‘b’, ‘c’];
var str = arr.join(‘ ‘);
console.log(str); // ‘a b c’
If you call the Array.join() method on an empty array, you’ll get an empty string:
var arr = [];
var str = arr.join(");
console.log(str); //""
If you call the Array.join() method on an array with undefined or null as one of the elements, that element will be converted to the empty string:
var arr = [‘a’, ‘b’, undefined, ‘c’, null];
var str = arr.join(");
console.log(str); //’abc’
Keep in mind that the Array.join() method does not change the original array. It simply returns a new string.
If you need to convert an array of values to a comma-separated string, you can even use the String() function passing the array as a parameter.
Here’s an example:
var arr = [‘a’, ‘b’, ‘c’];
var str = String(arr);
console.log(str); // ‘a,b,c’
Keep in mind that the String() function will add commas between the array elements.
Conclusion
The Array.prototype.join() method is the easiest way to convert an array to string without commas in JavaScript. If you want to use a different character (or characters) as a separator, pass that character (or characters) as the parameter to the Array.prototype.join() method.