Full answer
Arrays are ordered lists. The four basic methods work at the two ends and all mutate the array:
push(...items)adds to the end and returns the new length.pop()removes the last item and returns it (orundefinedfor an empty array).unshift(...items)adds to the start and returns the new length.shift()removes the first item and returns it.
Together, push and pop make a stack (last in, first out). push and shift make a queue (first in, first out). For large queues, shift is slow, because the engine may need to move every element one place (it is O(n) in the worst case), while push and pop are O(1). For big queues, keep an index pointer or use a different structure.
Knowing which methods mutate matters. These change the array: push, pop, shift, unshift, splice, sort, reverse, fill, and copyWithin. These return a new value and leave the array alone: slice, concat, map, filter, flat, toSorted, toReversed, and the spread syntax. A bug often comes from sort or reverse on shared data.
Searching: indexOf(value) returns the first index or -1, and uses strict equality, so it cannot find NaN. includes(value) returns true or false, uses SameValueZero, and finds NaN. Use includes for a yes or no answer, because if (arr.indexOf(x)) is a bug: index 0 is falsy, and -1 is truthy. Both accept a start position. For objects, they compare by reference, so use some or find with a condition.
Length: length is the highest index plus one and is writable: arr.length = 0 empties the array. at(-1) reads the last element. Use Array.isArray to test if a value is an array.
const stack = [];
console.log('push returns the new length:', stack.push('a', 'b', 'c'), [...stack]);
console.log('pop returns the removed item:', stack.pop(), [...stack]);
console.log('unshift returns the new length:', stack.unshift('z'), [...stack]);
console.log('shift returns the removed item:', stack.shift(), [...stack]);
console.log([].pop(), [].shift(), [].length);
const queue = [1, 2, 3];
queue.push(4);
console.log('queue order:', queue.shift(), queue.shift(), queue);
const letters = ['a', 'b', 'c'];
console.log('indexOf:', letters.indexOf('a'), letters.indexOf('z'), '| includes:', letters.includes('a'), letters.includes('z'));
console.log('if (indexOf) bug:', letters.indexOf('a') ? 'truthy' : 'falsy (index 0)', letters.indexOf('z') ? 'truthy (-1)' : 'falsy');
console.log('NaN:', [NaN].indexOf(NaN), [NaN].includes(NaN));
console.log('objects by reference:', [{ id: 1 }].includes({ id: 1 }), [{ id: 1 }].some((o) => o.id === 1));
console.log('start position:', [1, 2, 1].indexOf(1, 1), [1, 2, 1].includes(1, 3));
const original = [3, 1, 2];
const sortedCopy = original.toSorted();
console.log(sortedCopy, original);
original.sort();
console.log('sort changed the original:', original);
const items = [1, 2, 3, 4, 5];
items.length = 2;
console.log(items, items.at(-1));
// shift is O(n) on large arrays: an index pointer is a cheap queue
const big = Array.from({ length: 5 }, (_, i) => i);
let head = 0;
const take = () => big[head++];
console.log(take(), take(), big.slice(head));Run with Node.js v24.22.1 as js-154.js; two runs gave identical output. Output: push returns the new length: 3 [ 'a', 'b', 'c' ] pop returns the removed item: c [ 'a', 'b' ] unshift returns the new length: 3 [ 'z', 'a', 'b' ] shift returns the removed item: z [ 'a', 'b' ] undefined undefined 0 queue order: 1 2 [ 3, 4 ] indexOf: 0 -1 | includes: true false if (indexOf) bug: falsy (index 0) truthy (-1) NaN: -1 true objects by reference: false true start position: 2 false [ 1, 2, 3 ] [ 3, 1, 2 ] sort changed the original: [ 1, 2, 3 ] [ 1, 2 ] 2 0 1 [ 2, 3, 4 ]
Key points
- push and pop work at the end, unshift and shift at the start; all mutate the array.
- push and unshift return the new length; pop and shift return the removed element.
- shift and unshift are slower for large arrays; push and pop are fast.
- indexOf gives an index or -1 with strict equality; includes gives a boolean and finds NaN.
- Know which methods mutate (sort, reverse, splice) and which return new arrays.
Common mistakes
- Writing if (arr.indexOf(x)) instead of comparing with -1 or using includes.
- Using shift in a loop over a very large array.
- Calling sort or reverse on shared data and changing it for every user.
- Expecting includes or indexOf to compare objects by content.
Follow-ups
- What is the difference between splice and slice?
- How would you implement a queue with O(1) dequeue?
- Why can includes find NaN but indexOf cannot?
Tips
- Group the methods into mutating and non-mutating.
- Say what each method returns.
- Mention the performance cost of shift and unshift.