Last Updated: February 25, 2016
·
6.609K
· 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.

Have a fresh tip? Share with Coderwall community!

Post
Post a tip
Did you know?

JavaScript on a page runs with the privileges of that origin. One altered library, tag manager, or snippet can read inputs, change what people see, and call your APIs as the user.

Security insights from Cside