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!

3 Answers3

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);
Sascha
  • 4,576
  • 3
  • 13
  • 34
0

You can use concat:

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

console.log(m2,m4)
mplungjan
  • 169,008
  • 28
  • 173
  • 236
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)
brk
  • 48,835
  • 10
  • 56
  • 78
  • @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 – brk Aug 24 '20 at 14:57
  • @mplungjan well i too have almost 40K rep and i dont know what make you thought that i copied other two answers – brk Aug 24 '20 at 14:59
  • 1
    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. – brk Aug 24 '20 at 15:05
  • Ok, it was meant tongue in cheek. I will add the ;) next time – mplungjan Aug 24 '20 at 15:07