Home > OS >  Is there a way to combine all elements in an array?
Is there a way to combine all elements in an array?

Time:10-06

So I'm making a website, and I want to string together all the items in an array. Eg.

function combineArray {
const myArray = [text1, text2, text3];
//code to output text1text2text3 here
}

Does anyone know how to do this?

CodePudding user response:

join will join all elements in the array with the specified delimiter (use empty string '' since the default separator is comma ,)

const myArray = ["a", "b", "c"];
const res = myArray.join('');
console.log(res);

CodePudding user response:

You can use join('') to achieve what you are looking for.

const myArray = ["text1", "text2", "text3"];
const combineArray = myArray.join('');
console.log(combineArray);

documentation for join

  • Related