Technical skill practice
JavaScript Array pop: Read the Last Item or Remove It
Use at(-1) or length-based indexing to inspect the last array element. Use pop when you mean to remove and return it. Reading and consuming the newest item are different stack operations.
The task
Inspect the newest undo action, remove it, and verify the previous action remains. Check empty behavior separately instead of assuming every stack contains an item.
Runnable Node 22 example
const assert = require('node:assert/strict');
const undo = ['rename', 'move'];
assert.equal(undo.at(-1), 'move');
assert.equal(undo[undo.length - 1], 'move');
assert.equal(undo.length, 2);
const removed = undo.pop();
assert.equal(removed, 'move');
assert.deepEqual(undo, ['rename']);
assert.equal([].at(-1), undefined);
assert.equal([].pop(), undefined);
console.log(`removed=${removed}; next=${undo.at(-1)}; size=${undo.length}`);
Expected output and explanation
The two initial reads agree without changing the stack length. pop returns move and changes the container so rename becomes the last item. A function that merely wants a preview should not consume the action by calling pop.
The empty checks both produce undefined, but for different operations: one reads an absent position and one tries to remove from an empty array. If undefined is itself a permitted stack value, inspect length before removing when you need to distinguish those cases.
Output from the example
removed=move; next=rename; size=1
Pitfall to check
Array[-1] is an ordinary property lookup, not the negative-position behavior of at(-1). Do not substitute the bracket form with a literal −1 and assume it finds the last element.
Questions, answered
Does pop return the new array?
No. It returns the removed element, or undefined for an empty array. The original array is the container whose length changes.
Sources and examples
ResumizeAI team. Examples are fictional teaching material, not customer results. Replace sample details with your own facts.
Your experience. A complete resume.
Build in Resumize with ATS-friendly templates, guided editing and a live preview. Start free, then upgrade when you need more.
Free account required. Free-plan PDF exports include a watermark.