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
  • 28,450
  • 6
  • 38
  • 76
mm4096
  • 144
  • 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
  • 8,973
  • 3
  • 23
  • 37
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,344
  • 6
  • 10