Last Updated: February 25, 2016
·
6.343K
· thurloat

Javascript array mutability

Just a quick reminder to folks that some JS array functions have very
different behaviours and return types.

var x = ['foo', 'bar'];

array.push

x.push('baz');
=> 3 
// returns the index at which the item was inserted.

x
=> ['foo', 'bar', 'baz']
// modifies the array.

array.concat

x.concat('buzz');
=> ['foo', 'bar', 'baz', 'buzz']
// returns a new array with the the arguments combined.

x
=> ['foo', 'bar', 'baz']
// original array is untouched.