This page looks best with JavaScript enabled

Concat vs. Push for Arrays in Javascript

 ·   ·  ☕ 1 min read

Someone once said “there shall be standards, and there were standards”. But no one said anything about consistency, uniformity, and stuff like that?

The question is of course not about standards. Let me ask you this one thing - do you have any idea what’s going on with push and contact?

Here’s concat..

1
2
3
const nums = [1, 1, 2, 3];
console.log("nums.contact(): ", nums.concat(5)); // [ 1, 1, 2, 3, 5 ]
console.log("length: ", nums.length); // 4

And then, there’s push..

1
2
console.log("nums.push(): ", nums.push(5)); // 5
console.log("nums: ", nums); // [ 1, 1, 2, 3, 5 ]

Arrgghh.. how can they complement each other so perfectly!?

Stay in touch!
Share on

Prashanth Krishnamurthy
WRITTEN BY
Prashanth Krishnamurthy
Technologist | Creator of Things