Javascript - Mutation vs Immutability
📖 Read the lesson
Code Life
By Pierre Marsaa
Two Families of Methods
Imagine you call a method on your
team
array and later realize your original
team
was accidentally changed! Let's try to understand why.
In JavaScript, array methods belong to
two different families
:
-
Methods that return a new array
(like
.
map
()
,
.
filter
()
,
.
slice
()
) leave the original array completely untouched.
-
Methods that modify the original array
(like
.push()
,
.splice()
,
.sort()
) directly change the array they are called on.
↗️
Loading editor...
↶
▶️ Run
↷
Verify