1

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

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

Does anyone know how to do this?

Guerric P
  • 30,447
  • 6
  • 48
  • 86
mm4096
  • 153
  • 12
  • 2
    A possible solution would be to use `Array.prototype.join()` (Documentation: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/join) – tcconstantin Oct 06 '21 at 06:59

2 Answers2

3

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);
Ran Turner
  • 14,906
  • 5
  • 47
  • 53
1

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

ahsan
  • 1,409
  • 8
  • 11