freecoding.school100% FREE · NO SIGNUP
Closure CavesISSUE #19 of 61

array methods · push · pop · shift · unshift

LambdaVSThe Mutator
Lambda sayspush/pop work at the end; shift/unshift at the start — these mutate the array.

push adds to the end (returns new length), pop removes from the end (returns it). unshift/shift do the same at the front. All four mutate the array in place. End operations are fast; front operations re-index everything, so prefer push/pop in hot loops.

Power-ups you unlock

The Mutator attacks — common mistakes

Boss battleUse a push/pop pair to treat an array as a stack.

Example code

<!doctype html><html><head><meta charset="utf-8"></head>
<body style="background:#06040d;color:#e6e0ff;font-family:monospace;padding:20px"><pre id="o"></pre>
<script>const stack=[]; stack.push('a'); stack.push('b'); const top=stack.pop();
document.getElementById('o').textContent='popped '+top+' · left '+JSON.stringify(stack);</script></body></html>
▶ Open the interactive comic issue
‹ Arrays · Ordered ListsArray Methods · Slice · Splice ›