DEV Community

Basit Raza
Basit Raza

Posted on

Answer: Why use `concat` instead of `push` in this instance?

In simple push append the array in the same reference, while concat doestn't effect the orignal array. Check out following snippet

let x = [1,2,3,4];
let y = x.concat(5);
// At this step y hold [1,2,3,4,5] and x remain unchanged to [1,2,3,4]
document.write('x=>'+x);
document.write('y=>'+y);

let z = x.push(5);
…

Top comments (0)