JavaScript’s push, pop, shift, and unshift methods change array contents in clear, predictable ways. Follow how each call affects the order and length of an array, and see why these everyday tools matter when handling data in Salesforce applications and Lightning Web Components.

Multiple Choice

What will be printed after executing the following code: const arr = [1, 2, 3]; arr.push(6); console.log(arr); arr.pop(); console.log(arr); arr.shift(); console.log(arr); arr.unshift(8); console.log(arr);?

The code snippet executes several operations on the `arr` array, which initially contains the values `[1, 2, 3]`. 1. The first operation is `arr.push(6);`, which adds the value `6` to the end of the array. After this operation, the array becomes `[1, 2, 3, 6]`. The first `console.log(arr);` prints this modified array: `[1, 2, 3, 6]`. 2. Next, the code calls `arr.pop();`, which removes the last element from the array. After popping, the array goes back to its previous state of `[1, 2, 3]`. The second `console.log(arr);` outputs `[1, 2, 3]`. 3. The third operation, `arr.shift();`, removes the first element of the array. This shifts all remaining elements to the left, resulting in the array now being `[2, 3]`. The third `console.log(arr);` prints `[2, 3]`. 4. Finally, the code performs `arr.unshift(8);`, which adds the value `8` to the beginning of the array, transforming

JavaScript Array Methods: How push, pop, shift, and unshift Change an Array

Arrays are one of JavaScript’s most useful ways to store related values. A list of account names, a set of opportunity amounts, or a sequence of user interface messages can all fit neatly into an array. But an array isn’t just a container: JavaScript gives you methods to change what it holds, often with a single line of code.

Four methods show up often in everyday development: push(), pop(), shift(), and unshift(). They’re easy to mix up at first, so it helps to picture an array as a row of items. Two methods work at the end of the row; the other two work at the beginning. Once that distinction clicks, their behavior becomes much easier to follow.

Two ends, four methods

Here’s the quick map:

| Method | What it does |

|---|---|

| push(value) | Adds one or more values to the end |

| pop() | Removes the last value |

| unshift(value) | Adds one or more values to the beginning |

| shift() | Removes the first value |

Each method changes the original array. That detail matters. If a variable refers to the array, it will see the updated contents after one of these methods runs.

Consider a simple starting point:


const arr = [1, 2, 3];

The array contains three values in order. Now imagine adding 6 to the end:


arr.push(6);

console.log(arr);

The output is:


[1, 2, 3, 6]

push() places the new value after the existing last item. It does not create a separate array by default; it updates arr itself.

Add at the end with push()

The name push() hints at the action: a value gets pushed onto the end of the list. You can add more than one value in a single call, too:


const colors = ["blue", "green"];

colors.push("red", "gold");

console.log(colors);

Now colors contains:


["blue", "green", "red", "gold"]

The order of the added values stays the same. This can be handy when a component gathers new items over time, such as a list of selected records or status messages.

There’s another useful detail: push() returns the array’s new length. So, after adding a value, you can capture the count:


const items = ["pen", "notebook"];

const count = items.push("folder");

console.log(count); // 3

The method changes the array, while its return value is the number of items—not the modified array. Keep those two facts separate and the code is easier to read.

Remove from the end with pop()

pop() removes the final item from an array:


const arr = [1, 2, 3, 6];

arr.pop();

console.log(arr);

The array is now:


[1, 2, 3]

Like push(), pop() changes the original array. It also returns a value: the item it removed. That makes it useful when the removed value matters to the rest of the code.


const tasks = ["review record", "send update"];

const finishedTask = tasks.pop();

console.log(finishedTask); // "send update"

console.log(tasks);        // ["review record"]

If the array is empty, calling pop() returns undefined. That’s not an error, but it may be worth checking in code where an empty list is possible.


const emptyList = [];

console.log(emptyList.pop()); // undefined

Think of push() and pop() as a pair for the end of the array: one adds there, and one removes there.

Remove from the beginning with shift()

To remove the first item instead, JavaScript provides shift():


const arr = [1, 2, 3];

arr.shift();

console.log(arr);

The result is:


[2, 3]

The first value is gone, and the remaining values keep their order. You can picture them sliding one place to the left, though JavaScript handles the underlying work for you.

shift() returns the value it removed. As with pop(), it returns undefined when the array has no items.

This method can make sense when items are handled in arrival order—for instance, when code processes the oldest item in a simple queue. Still, repeatedly removing the first item from a large array may involve more work than removing the last one, since the remaining values need new positions. For small arrays, that’s rarely a concern. For larger data structures, it’s useful to know what’s happening under the hood.

Add at the beginning with unshift()

The partner to shift() is unshift(). It adds one or more values at the start:


const arr = [2, 3];

arr.unshift(8);

console.log(arr);

The updated array is:


[8, 2, 3]

Just like push(), unshift() can add several values:


const numbers = [3, 4];

numbers.unshift(1, 2);

console.log(numbers); // [1, 2, 3, 4]

The values appear in the same order as they were passed in. unshift() returns the array’s new length, not the array itself. And, like shift(), it changes the original array.

Follow the array, one line at a time

When several methods run in sequence, the key is to track the array’s current contents after each change. Start with:


const arr = [1, 2, 3];

Then follow the operations:


arr.push(6);

console.log(arr); // [1, 2, 3, 6]

arr.pop();

console.log(arr); // [1, 2, 3]

arr.shift();

console.log(arr); // [2, 3]

arr.unshift(8);

console.log(arr); // [8, 2, 3]

Each console.log() runs at a particular moment. It shows the array’s state at that point in the sequence. The first output doesn’t change later just because the array changes afterward; the console call has already happened. In browser developer tools, though, displayed objects can sometimes be shown in ways that reflect later changes, depending on the tool and how the value is inspected. For simple checks, logging a copied array can make the snapshot clearer:


console.log([...arr]);

That spread syntax creates a shallow copy of the array for logging. It’s a small distinction, but it can save head-scratching while debugging.

A note about const and mutation

The example uses const to declare arr, yet the array changes several times. That’s allowed. const prevents reassignment of the variable, but it doesn’t freeze the contents of an array or object.

This works:


const names = ["Ari"];

names.push("Sam");

This does not:


const names = ["Ari"];

names = ["Sam"]; // TypeError

In the first snippet, names still points to the same array; its contents have changed. In the second, the code tries to make names point to a different array. That’s a reassignment, which const blocks.

This difference matters in Salesforce JavaScript, too. A component may hold arrays of records or UI state, and changing an array in place can affect how updates are detected or rendered. Framework patterns vary, so follow the guidance for the specific Salesforce environment you’re using. In some cases, creating a new array is clearer:


const updatedNames = [...names, "Sam"];

That expression leaves the original array untouched and makes a new one. It’s a different approach, not a replacement for knowing how the four methods work.

Keep the mental model simple

push() and pop() work at the end. unshift() and shift() work at the beginning. The adding methods return the new length; the removing methods return the value they removed. All four change the original array.

That’s the whole pattern. When code feels hard to trace, write down the array after each line or add a brief comment beside each log. A list changes one step at a time, and JavaScript doesn’t skip ahead. Once you keep that sequence in view, these methods become less like four names to memorize and more like a small, reliable toolkit.