Javascript - Modifying Elements
📖 Read the lesson
Code Life
By Pierre Marsaa
Replacing an Element
To
read
an element, you write the array name and an index between brackets:
team[
1
]
gives you the value stored at index 1.
To
edit
an element, you use exactly the same expression:
team[
1
] =
"Zap"
stores a new value at index 1.
Loading editor...
Here Pyrochu, at index 1, is swapped out for Zap:
↗️
Loading editor...
↶
▶️ Run
↷
Pyrochu is out, Zap is in. The array still has 3 elements: the value at index 1 was simply replaced.
const, again
team
is a
const
, and this works.
const
blocks
team = ...
, not
team[
1
] = ...
: the variable still points to the same array, only its content changed.
Verify