Question
Should I use .push
or []
notation to add an element to the end of my array?
Answer
It is advisable to use .push
because with []
notation the size of your array might change later and you might be adding a value to an already occupied index. .push
guarantees that you’ll be adding to the end of your array. An alternative if you want to use []
notation would be to use .length
with []
notation like so:
javascript let a = [1, 2]; a[a.length] = 3;