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?

1

2 Answers 2

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);

2
  • 3
    toString() is redundant, it's already a string.
    – mbojko
    Commented Oct 6, 2021 at 7:16
  • Modified, thanks.
    – Ran Turner
    Commented Oct 6, 2021 at 7:34
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

2
  • 2
    Think about using join('').
    – ceving
    Commented Oct 6, 2021 at 7:17
  • Thanks didn't know that, updated my answer
    – ahsan
    Commented Oct 6, 2021 at 7:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.