Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which JavaScript Array functions are mutating?

I am writing an Array-derived class in JavaScript and need to know which functions to overload so that I can be aware of changes made to the array.

I know Array.push() and Array.splice() are mutating. Is there a definitive list of any others?

like image 758
devios1 Avatar asked Jan 25 '12 20:01

devios1


People also ask

Which JavaScript function is non mutating?

filter() , the filter() is among the coolest non-mutating method on the array.

Are arrays mutable in JavaScript?

In JavaScript, only objects and arrays are mutable, not primitive values.

Is array push mutating?

Generally not. There are times when treating data as immutable is useful, or even essential (such as when you are updating a Redux store). In those cases, push still isn't a bad idea, you just shouldn't do it to your original array.


1 Answers

You can find the list on MDN as Mutator methods (along with Accessor and Iteration methods):

  • copyWithin
  • fill
  • pop
  • push
  • reverse
  • shift
  • sort
  • splice
  • unshift
like image 69
Jonathan Lonowski Avatar answered Oct 02 '22 10:10

Jonathan Lonowski