0

I have two array:

let myArray1 = [1, 2, 3];
let myArray2 = [9, 5, 6];
 

I try to merge the two array and save to a new array using push. I am aware that I can easily do it with concat. But I want to understand more about the code.

let myArray1 = [1, 2, 3];
let myArray2 = [9, 5, 6];

let myArray3 = myArray1.push(myArray2); // Result 4

I don't understand why it is 4 for the output?

DecPK
  • 24,537
  • 6
  • 26
  • 42
  • 1
    After the push, your array will be `[1, 2, 3, [9, 5, 6]]`. 4 items. – spender May 20 '21 at 13:02
  • `4` is returned because `Array.prototype.push` returns -> `The new length property of the object upon which the method was called.` – DecPK May 20 '21 at 13:02
  • If you want a new array`let myArray3 = myArray1.concat(myArray2)` . If you want to push into myArray1: `myArray1.push(...myArray2)`. push returns the updated length of the array. – adiga May 20 '21 at 13:03

0 Answers0