0

I am pretty new to Javascript, so this might be a silly question. but is it possible to add arrays together? like... var m2 = new Array(2).fill(TEST); var m4 = m2 + m2;

or is there another technique in which i can do such a thing? Thank you!

1

3 Answers 3

0

One possibility is to use the spread operator ... .

let arr1 = [1,2,3,4];
let arr2 = ['a','b','c'];

let array = [...arr1, ...arr2];
console.log(array);

Sign up to request clarification or add additional context in comments.

Comments

0

You can use concat:

const m2 = new Array(2).fill("TEST");  
const m4 = m2.concat(m2);

console.log(m2,m4)

Comments

0

Joing two array like []+[] will result to a string. You can merge two array using e destructuring or using concat

var m2 = new Array(2).fill('TEST');
var m4 = [...m2, ...m2];
var m5 = m2.concat(m2)
console.log(m4);
console.log(m5)

4 Comments

@mplungjan not really may be you posted just before me & i was not yet to answer but since with 116K you answered a duplicate question i opted to answer too
@mplungjan well i too have almost 40K rep and i dont know what make you thought that i copied other two answers
Your first comment was not necessary and when stack snippet is open know one will able to see if any answer is already posted. You may not have written it but i feel you intended to mean i looked other two answers before writing mine. Anyway Thanks for your comment.
Ok, it was meant tongue in cheek. I will add the ;) next time

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.