I’m sure somebody has written about this, but I couldn’t find anything so I’ll share with you what I know.
First, let me define what I mean when I say mutative:
|unshift, push||every *|
NB: the functional methods marked with a * can be mutative if the function you pass to them modifies the original array. In my expierience this has been especially true in the case of Array.prototype.forEach.
Tips for telling which Array.prototype methods are mutative
- Just glancing at this list, we can see that an even 1/3 of array methods are mutative, so you can assume most array methods are non-mutative.
- All functional methods (marked with a *) are non-mutative.
- All operations modifying the beginning and end of an array are mutative (push, pop, shift, unshift).
- The only other mutative array methods are splice, reverse, and sort. If you can remember those three and the above rules, telling array methods apart should become a lot easier.