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.
Written by Adam Thurlow
Related protips
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Array
Authors
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#