Computer Programming 1 (JavaScript) • Core Data Structures

Array Methods — I can add/remove elements using common methods (push, pop, shift, splice) at my level.

Master the essential toolkit for dynamically adding and removing elements from JavaScript arrays.

A JavaScript data file with code brackets and a laptop.
Inside this lesson 10 sections 5 questions 9 figures

SECTION 1

Historical Context & Motivation

Before programming languages gave us convenient tools, working with collections of data was tedious and error-prone. Early programmers had to manually track how many items were in a list, allocate fixed blocks of memory, and shift data around by hand whenever they wanted to insert or remove something. The development of array methods — built-in functions attached to arrays — was a game-changer that let developers manipulate lists of data with a single line of code instead of dozens.

JavaScript, created in 1995 by Brendan Eich, inherited the concept of arrays from earlier languages like C and Java. However, JavaScript arrays were designed to be far more flexible: they can grow and shrink dynamically, hold mixed data types, and come equipped with powerful built-in methods. Over time, the language standardized these methods through the ECMAScript specification, ensuring that push, pop, shift, unshift, and splice behave consistently across every browser and runtime environment.

1957 • FORTRAN Introduces Arrays

FORTRAN became one of the first high-level languages to offer fixed-size arrays, letting scientists store sequences of numbers without managing raw memory addresses.

1995 • JavaScript Is Born

Brendan Eich creates JavaScript in 10 days at Netscape. Arrays are included from the start as dynamic, resizable objects with built-in methods like push and pop.

1997 • ECMAScript 1 Standardized

The first ECMAScript specification formalizes array behavior, including methods for adding and removing elements, ensuring cross-browser consistency.

2009 • ES5 Expands Array Methods

ECMAScript 5 adds forEach, map, filter, and reduce — but push, pop, shift, and splice remain the foundational tools for modifying array contents in place.

The core question these methods answer is simple but essential: How do I efficiently add or remove items from a list without writing complex, error-prone code? Understanding push, pop, shift, and splice gives you the power to build dynamic applications — from to-do lists to game inventories to social media feeds — where data is constantly being created, modified, and deleted.

SECTION 2

Core Principles & Definitions

Before diving into each method, you need to understand a few foundational ideas. A JavaScript array is an ordered collection of values, where each value sits at a numbered position called an index. Indexes start at 0, so the first element is at index 0, the second at index 1, and so on. Array methods are functions that are built into every array object, and you call them using dot notation — for example, myArray.push("hello").

1

Mutating vs. Non-Mutating

Methods like push, pop, shift, and splice are mutating — they change the original array directly rather than creating a new copy. This is important to remember when debugging.
2

End vs. Beginning

push and pop operate on the end of an array, while shift and unshift operate on the beginning. Knowing which end you're modifying prevents common index errors.
3

Return Values Matter

Each method returns something useful. push returns the new length of the array, pop returns the removed element, and splice returns an array of removed elements. Always check what a method returns.
4

Splice: The Swiss Army Knife

While push/pop/shift handle the ends, splice can insert, remove, or replace elements at any position in the array. It is the most versatile — and the most complex — of the four.
✦

KEY TAKEAWAY

Think of an array like a line of students in a hallway. push adds a student to the back of the line. pop removes the student at the back. shift removes the student at the front, causing everyone else to shuffle forward. splice lets you pull someone out of the middle — or insert someone anywhere — while everyone else adjusts positions automatically.

SECTION 3

Visual Explanation — How Each Method Works

The diagram below illustrates a five-element array and shows exactly what happens when you call each of the four core methods. Pay close attention to which end of the array is affected and what value each method returns. The arrows indicate the direction of the operation — whether the element is being added or removed, and from which side.

Array: ["A", "B", "C", "D", "E"]idx 0idx 1idx 2idx 3idx 4"A""B""C""D""E".push("F")Adds "F" to the END → returns new length: 6"F"← added at end.pop()Removes last element → returns "E""E" ✕← removed from end.shift()Removes first element → returns "A"; all others shift left"A" ✕← removed from front; B,C,D,E slide to indexes 0−3.splice(2, 1, "X")At index 2, remove 1 element, insert "X" → returns ["C"]"C" → "X"↑ replaced at index 2
The diagram shows the original five-element array at the top, then four rows illustrating how push adds to the end, pop removes from the end, shift removes from the front, and splice can target any index for removal, insertion, or replacement.

Notice how push and pop only touch the last position, which means the indexes of all other elements remain unchanged. In contrast, shift causes every remaining element to move one index to the left. This distinction matters for performance in large arrays, but at this stage, the most important thing is understanding which end of the array each method targets and what value it returns.

SECTION 4

How Each Method Works — Syntax & Behavior

Let's break down the exact syntax and behavior of each method. Understanding the parameters each method accepts and the value it returns is the key to using them confidently. Every example below starts with let fruits = ["apple", "banana", "cherry"].

push( ) — Add to the End

SYNTAX

array.push(element1,element2,...elementN)array.push(element1, element2, ...elementN)

Accepts one or more values to append. Returns the new length of the array.

Calling fruits.push("date") adds "date" to the end. The array becomes ["apple", "banana", "cherry", "date"] and push returns 4 — the new length. You can push multiple items at once: fruits.push("elderberry", "fig") would append both, returning 6.

pop( ) — Remove from the End

SYNTAX

array.pop()array.pop()

Takes no arguments. Returns the removed element. If the array is empty, returns undefined.

Calling let removed = fruits.pop() removes "cherry" from the end. The variable removed now holds "cherry", and the array is ["apple", "banana"]. Notice how pop and push are perfect complements — push adds to the end, pop removes from the end.

shift( ) — Remove from the Beginning

SYNTAX

array.shift()array.shift()

Takes no arguments. Returns the removed first element. All remaining elements shift down by one index.

Calling let first = fruits.shift() removes "apple" from index 0. Now first holds "apple", and the array becomes ["banana", "cherry"]. The companion method unshift does the opposite — it inserts elements at the beginning, using the syntax array.unshift(element1, ...elementN), and returns the new length.

splice( ) — Add, Remove, or Replace Anywhere

SYNTAX

array.splice(startIndex,deleteCount,item1,item2,...itemN)array.splice(startIndex, deleteCount, item1, item2, ...itemN)

startIndex: position to begin changes. deleteCount: number of elements to remove (use 0 to insert without removing). items: optional elements to insert at startIndex. Returns an array of removed elements.

Splice is the most powerful of the four methods because it can do three things: remove elements, insert elements, or do both simultaneously. Calling fruits.splice(1, 1) starts at index 1 and removes 1 element — "banana" — leaving ["apple", "cherry"]. Calling fruits.splice(1, 0, "blueberry") starts at index 1, removes 0 elements, and inserts "blueberry", yielding ["apple", "blueberry", "banana", "cherry"]. Calling fruits.splice(1, 1, "blackberry") replaces "banana" with "blackberry".

SECTION 5

Detailed Comparison — Which Method to Use When

Choosing the right method depends on two questions: Where in the array do you want to make changes? and Do you want to add, remove, or replace? The diagram and table below will help you navigate these decisions quickly.

Decision Flowchart: Which Array Method?What do you need to do?AddRemoveReplaceWhere?Where?Use splice(i, 1, newVal)EndStartMiddle.push().unshift().splice(i,0,v)EndStartMiddle.pop().shift().splice(i,n)Tip: splice() can handle ALL cases — but simpler methods are clearer for end/start operations.
This flowchart guides you through choosing the right array method based on your goal (add, remove, or replace) and the target position (beginning, end, or middle).
MethodActionPositionReturns
push(val)Add element(s)EndNew array length
pop()Remove one elementEndRemoved element
shift()Remove one elementBeginningRemoved element
unshift(val)Add element(s)BeginningNew array length
splice(i, n, ...vals)Add, remove, or replaceAny indexArray of removed elements

Summary of the five core mutating array methods

SECTION 6

Worked Example — Building a Playlist

Imagine you're building a music playlist app. You start with three songs and need to add, remove, and rearrange tracks. Let's walk through each operation step by step, tracking the array state after every change.

Playlist Manager

  1. 1

    Step 1 — Initialize the Playlist

    Start by creating the array: let playlist = ["Bohemian Rhapsody", "Imagine", "Hey Jude"]; The array has 3 elements at indexes 0, 1, and 2.
    playlist → ["Bohemian Rhapsody", "Imagine", "Hey Jude"] (length: 3)
  2. 2

    Step 2 — Add a Song to the End with push()

    A user adds a new song: let newLength = playlist.push("Stairway to Heaven"); The method appends the song to the end and returns the new length.
    playlist → ["Bohemian Rhapsody", "Imagine", "Hey Jude", "Stairway to Heaven"] | newLength → 4
  3. 3

    Step 3 — Remove the Last Song with pop()

    The user changes their mind and removes the last song: let removed = playlist.pop(); Pop returns the removed element so you can display a confirmation message.
    removed → "Stairway to Heaven" | playlist → ["Bohemian Rhapsody", "Imagine", "Hey Jude"]
  4. 4

    Step 4 — Remove the First Song with shift()

    The first song in the queue finishes playing, so we remove it: let played = playlist.shift(); All remaining elements slide forward — "Imagine" is now at index 0 and "Hey Jude" is at index 1.
    played → "Bohemian Rhapsody" | playlist → ["Imagine", "Hey Jude"]
  5. 5

    Step 5 — Insert a Song in the Middle with splice()

    The user wants to insert "Let It Be" between the two remaining songs. We use splice at index 1, removing 0 elements, and inserting the new track: playlist.splice(1, 0, "Let It Be"); Since deleteCount is 0, nothing is removed — the new song simply slots in, pushing "Hey Jude" to index 2.
    playlist → ["Imagine", "Let It Be", "Hey Jude"] (length: 3)
  6. 6

    Step 6 — Replace a Song with splice()

    Finally, the user wants to swap "Hey Jude" (at index 2) for "Yesterday": let swapped = playlist.splice(2, 1, "Yesterday"); Splice removes 1 element at index 2 and inserts "Yesterday" in its place. It returns an array containing the removed element.
    swapped → ["Hey Jude"] | playlist → ["Imagine", "Let It Be", "Yesterday"]

SECTION 7

Strengths, Pitfalls & Common Mistakes

Each method has clear strengths, but they also come with common pitfalls that can trip up beginners. The table below summarizes the key advantages and the mistakes students most frequently make when using these methods.

MethodStrengthCommon Mistake
push()Simple, fast, and the most common way to grow an arrayTrying to use the return value as the array itself — push returns the new length, not the modified array
pop()Cleanly retrieves and removes the last element in one callCalling pop() on an empty array doesn't throw an error — it silently returns undefined, which can cause bugs later
shift()Perfect for queue-like behavior (first in, first out)Forgetting that all remaining indexes change after shift — a loop counter can skip elements
splice()Extremely versatile — handles insertion, deletion, and replacement at any positionConfusing splice (mutates the array) with slice (returns a copy without changing the original)

Strengths and common pitfalls for each array method

⚠

splice vs. slice — Don't Mix Them Up!

splice (with a 'p') changes the original array by adding or removing elements. slice (without the 'p') creates a new array from a portion of the original without modifying it. A helpful mnemonic: splice = changes the array.
✦

KEY TAKEAWAY

Think of these methods like managing a queue at a coffee shop. push is like a new customer joining the back of the line. pop is like the last person giving up and leaving. shift is the barista serving the person at the front. And splice is when a VIP cuts into the middle of the line — or someone gets pulled out of it entirely.

SECTION 8

Connection to Advanced Concepts

The four methods you've learned are mutating methods — they change the original array. As you advance in JavaScript, you'll encounter a complementary set of non-mutating methods (like slice, concat, map, and filter) that return new arrays and leave the original untouched. Modern JavaScript frameworks like React often prefer non-mutating approaches for reasons related to state management and rendering efficiency.

ConceptWhat You Know NowWhat Comes Next
Adding elementspush() / unshift()Spread operator: [...arr, newItem]
Removing elementspop() / shift() / splice()filter() to create a new array without certain items
Transforming elementsSplice to replace one-by-onemap() to transform every element into a new array
Data structuresArrays as simple listsStacks (push/pop), Queues (push/shift), and linked lists

How today's methods connect to more advanced techniques

An especially powerful idea is that push and pop together give you a stack — a last-in, first-out (LIFO) data structure used in function call tracking, undo features, and expression evaluation. Meanwhile, push and shift together give you a queue — a first-in, first-out (FIFO) data structure used for task scheduling and message handling. You're already building the foundations for these computer science concepts without even realizing it.

SECTION 9

Practice Problems

Question 1: CONCEPTUAL

You have an array let colors = ["red", "green", "blue"]. You call colors.push("yellow"). What does the method call return, and what does the array look like afterward?
Show answer
The push() method returns the new length of the array, which is 4. The array is now ["red", "green", "blue", "yellow"]. Push always adds to the end and returns the updated length — not the array itself. This is a common source of confusion, so always remember that the return value is a number.

Question 2: BASIC CALCULATION

Given let nums = [10, 20, 30, 40, 50], write two lines of code: first, remove the last element; then, remove the first element. After both operations, what is the array, and what values were returned by each method call?
Show answer
Line 1: let last = nums.pop(); — removes 50 from the end. last is 50. The array is now [10, 20, 30, 40]. Line 2: let first = nums.shift(); — removes 10 from the beginning. first is 10. The final array is [20, 30, 40]. Both pop and shift return the removed element — pop returns 50, shift returns 10.

Question 3: INTERMEDIATE

You have let animals = ["cat", "dog", "fish", "bird", "snake"]. Using a single call to splice(), remove "fish" and "bird" (indexes 2 and 3) and replace them with "hamster" and "rabbit". What does splice return, and what is the final array?
Show answer
Call: let removed = animals.splice(2, 2, "hamster", "rabbit"); The first argument (2) is the start index, the second argument (2) is the number of elements to delete, and the remaining arguments are the new elements to insert. The method returns ["fish", "bird"] — an array of the two removed elements. The final array is ["cat", "dog", "hamster", "rabbit", "snake"]. Notice that you can remove and insert different numbers of elements — splice handles the re-indexing automatically.

Question 4: APPLIED

You're building a simple task manager. Start with let tasks = ["homework", "laundry", "dishes"]. Write the code to: (a) add "groceries" to the end, (b) mark the first task as done by removing it, (c) insert "exercise" at index 1, and (d) remove "dishes" wherever it currently is. Show the array after each step.
Show answer
(a) tasks.push("groceries"); → ["homework", "laundry", "dishes", "groceries"]. (b) tasks.shift(); → ["laundry", "dishes", "groceries"]. (c) tasks.splice(1, 0, "exercise"); → ["laundry", "exercise", "dishes", "groceries"]. (d) "dishes" is now at index 2, so: tasks.splice(2, 1); → ["laundry", "exercise", "groceries"]. In real code, you could use tasks.indexOf("dishes") to dynamically find the index before splicing.

Question 5: CRITICAL THINKING

A student writes the following code and expects result to be the modified array: let result = [1, 2, 3].push(4); They then try result.pop() and get an error. Explain why this fails, and rewrite the code so that result ends up as [1, 2, 3, 4] with the ability to call pop on it.
Show answer
The error occurs because push() returns the new length of the array (the number 4), not the array itself. So result is set to 4 — a plain number. Calling result.pop() on a number throws a TypeError because numbers don't have array methods. The fix is to separate array creation from the push call: let result = [1, 2, 3]; result.push(4); Now result is still an array reference, and result.pop() will correctly remove and return 4. This illustrates a core principle: always know what your method returns before chaining or assigning it.

SUMMARY

Lesson Summary

Summary

JavaScript provides four essential mutating array methods for adding and removing elements. push() appends one or more elements to the end of an array and returns the new length. pop() removes and returns the last element. shift() removes and returns the first element, causing all remaining items to re-index. splice() is the most versatile — it can insert, remove, or replace elements at any position using the syntax splice(startIndex, deleteCount, ...items).

Remember that all four methods modify the original array in place rather than creating a new one. Always pay attention to return values: push and unshift return the new length, while pop, shift, and splice return the removed element(s). Together, push and pop form a stack (LIFO), while push and shift form a queue (FIFO) — foundational data structures you'll explore as you advance in computer science.

Varsity Tutors • Computer Programming 1 (JavaScript) • Array Methods — I can add/remove elements using common methods (push, pop, shift, splice) at my level.