Use the join() method or toString() method to convert an array to a string with commas in JavaScript. And if you want to convert an array of objects into a string, then use the JSON.stringify
method:
arr.join();
You can specify an empty string as an argument to join, if no argument is specified a comma is used.
Example JavaScript array to string with commas
A simple example code join()
method returns an array as a string.
<!DOCTYPE html>
<html>
<head>
<script>
let arr = ["ABC", "Is", "Popular","Language"];
let res = arr.join();
console.log(res);
console.log(typeof(res))
</script>
</head>
</html>
Output:
The method array.toString()
actually calls array.join()
which results in a string concatenated by commas.
var array = ['a','b','c','d','e','f'];
document.write(array.toString());
Output: “a,b,c,d,e,f”
Do comment if you have any doubts or suggestions on this JS convert array to string code.
Note: The All JS Examples codes are tested on the Firefox browser and the Chrome browser.
OS: Windows 10
Code: HTML 5 Version