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
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
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#