The Array.prototype.join() method:
var arr = ["Zero", "One", "Two"];
document.write(arr.join(", "));
Answer from Wayne on Stack OverflowThe Array.prototype.join() method:
var arr = ["Zero", "One", "Two"];
document.write(arr.join(", "));
Actually, the toString() implementation does a join with commas by default:
var arr = [ 42, 55 ];
var str1 = arr.toString(); // Gives you "42,55"
var str2 = String(arr); // Ditto
I don't know if this is mandated by the JS spec but this is what most pretty much all browsers seem to be doing.
How to convert array into comma separated string in javascript - Stack Overflow
jquery - Array to string containing comma's - JavaScript - Stack Overflow
Convert array to string javascript by removing commas - Stack Overflow
How do I convert an array to a string with commas in JavaScript
Videos
In JavaScript, you can convert an array to a string with commas using the join()
method.
The join() method returns a string that concatenates all the elements of an array, separated by the specified separator, which in this case is a comma.
Here is an example:
const array = ['apple', 'banana', 'orange'];
const string = array.join(', ');
console.log(string);
// output: "apple, banana, orange"
In this example, we first define an array of three fruits. Then we use the join()
method with a comma and a space as the separator to create a string that lists all the fruits with a comma and a space between each one.
You can replace the comma and space separator with any other separator you like, such as a hyphen, a semicolon, or a newline character.
It's important to note that the join() method only works on arrays, and it will throw an error if you try to use it on any other type of object.
Click here to learn more ways to Convert Array to String with Commas in JS
The method array.toString() actually calls array.join() which result in a string concatenated by commas. ref
var array = ['a','b','c','d','e','f'];
document.write(array.toString()); // "a,b,c,d,e,f"
Also, you can implicitly call Array.toString() by making javascript coerce the Array to an string, like:
//will implicitly call array.toString()
str = ""+array;
str = `${array}`;
Array.prototype.join()
The join() method joins all elements of an array into a string.
Arguments:
It accepts a separator as argument, but the default is already a comma ,
str = arr.join([separator = ','])
Examples:
var array = ['A', 'B', 'C'];
var myVar1 = array.join(); // 'A,B,C'
var myVar2 = array.join(', '); // 'A, B, C'
var myVar3 = array.join(' + '); // 'A + B + C'
var myVar4 = array.join(''); // 'ABC'
Note:
If any element of the array is undefined or null , it is treated as an empty string.
Browser support:
It is available pretty much everywhere today, since IE 5.5 (1999~2000).
References
- ECMA Specification
- Mozilla
- MSDN
Use the join method from the Array type.
a.value = [a, b, c, d, e, f];
var stringValueYouWant = a.join();
The join method will return a string that is the concatenation of all the array elements. It will use the first parameter you pass as a separator - if you don't use one, it will use the default separator, which is the comma.
When you call .join() on the array in order to convert it to a string yo ucan specify the delimiter.
For example:
["Hello,","World"].join("%%%"); // "Hello,%%%World"
You can then split it based on "%%%" (.split("%%%")) in order to break it apart again.
That said, if you want to apply an action to each element of an array (every line) you probably do not have to call .join and then .split it again. Instead, you can use array methods, for example:
var asLower = ["Hello","World"].map(function(line){ return line.toLowerCase(); });
// as Lower now contains the items in lower case
Alternatively, if your goal is serialization instead of processing - you should not "roll your own" serialization and use the built in JSON.parse and JSON.stringify methods like h2oooooo suggested.
You can use JSON.stringify(array) and JSON.parse(json) to make sure that whatever array/object you enter will come back the exact same (and it also works with booleans, integers, floats, etc.):
var data = ["This is a normal string", "This string, will cause a conflict.", "This string should be normal"];
// The data gets sent to a background script, in string form and comes back as this
data = JSON.stringify(data);
console.log(data);
// ["This is a normal string","This string, will cause a conflict.","This string should be normal"]
data = JSON.parse(data);
console.log(data);
// ["This is a normal string", "This string, will cause a conflict.", "This string should be normal"]
You can simple use the Array.prototype.join function
const A = [ '6', '6', '.', '5' ];
console.log(A.join(''));
As explain on the Documentation page of Array.prototype.join
The join() method creates and returns a new string by concatenating all of the elements in an array (or an array-like object), separated by commas or a specified separator string. If the array has only one item, then that item will be returned without using the separator.
It can take as parameter the separator which will be use to separate element from the array when they are joined.
let list = ["a", "b", "c", "d"];
list.join("-"); // will return "a-b-c-d"
list.join("/"); // will return "a/b/c/d"
list.join(""); // will return "abcd"
By default the separator is ,. This means if you don't specify which character will be use as the separator it will use the , character
list.join(); // will return a,b,c,d
console.log(['6', '6', '.', '5'].join(''));