js turn array into string code example

Example 1: javascript array to comma separated string

var colors = ["red", "blue", "green"];
var colorsCSV = colors.join(","); //"red,blue,green"

Example 2: js array to string

var myArray = ['no','u'];
var myString = myArray.toString();

Example 3: .join in javascript

const elements = ['Sun', 'Earth', 'Moon'];

console.log(elements.join());
// output: "Sun,Earth,Moon"

console.log(elements.join(''));
// output: "SunEarthMoon"

console.log(elements.join('-'));
// output: "Sun-Earth-Moon"

Example 4: transform array to string js

//Use array.join(separator) //
const myArray = [1, 2, 3, 4, 5, 6];
console.log(a.join('/'));
//output : 1/2/3/4/5/6
const myArray = ['g', 'o', 'o', 'g', 'l', 'e'];
console.log(a.join(''));
//output : 'google'
// Source : https://www.geeksforgeeks.org/javascript-array-join-method/#:~:text=Below%20is%20the%20example%20of%20the%20Array%20join()%20method.&text=The%20arr.,is%20a%20comma(%2C%20).

//Other function : array.toString() //
const array1 = [1, 2, 'a', '1a'];
console.log(array1.toString());
// output: "1,2,a,1a"
// Source : https://developer.mozilla.org/fr/docs/Web/JavaScript/Reference/Objets_globaux/Array/toString

Example 5: javascript join list of string

// array.join(separator)

var myArray ['foo', 'bar', 'baz'];
myarray.join(':');
// foo:bar:baz

Example 6: array to string javascript

<array>.join(<splitting character(s)>);

Tags:

Java Example