Avoid Array.prototype.push

Avoid Array.prototype.push. It modifies the array in place, which is asking for trouble. It also returns the array length, not the modified array, which is plain confusing.

Instead, prefer the array spread syntax1.

const original = ['cat', 'sat']
const modified = [...original, 'mat']

If you need to go old school, there’s Array.prototype.concat.

const original = ['cat', 'sat']
const modified = original.concat(['mat'])

Footnotes

  1. Unless you’re dealing with a massive array, where memory usage is an issue.

Sign up for my newsletter

A monthly round-up of blog posts, projects, and internet oddments.