JavaScript Array Methods Explained with Examples
JavaScript Array Methods Explained: pop(), shift(), unshift(), splice(), map(), forEach()
JavaScript array methods are very important for day-to-day coding, interviews, and frontend development. If you are working with lists of data, then methods like pop(), shift(), unshift(), splice(), map(), and forEach() will be used again and again.
In this article, we will understand what these methods do, when to use them, and how they work with simple examples. The explanation is kept easy so that beginners can understand properly.
Why These Array Methods Are Important
Arrays are used to store multiple values in JavaScript. For example, a list of students, products, marks, cities, or users can all be stored inside an array.
These methods help you add, remove, replace, loop through, and transform array data easily.
1. pop()
The pop() method removes the last element from an array and returns that removed value.
What it does
Removes the last item from the array.
Use case
Useful when you want to delete or process the last element.
let emotions = ["π", "π±", "π"];
let lastEmotion = emotions.pop();
console.log(emotions); // ["π", "π±"]
console.log(lastEmotion); // "π"
Here, pop() removed the last element "π" from the array and returned it.
2. shift()
The shift() method removes the first element from an array and returns it.
What it does
Removes the first item from the array.
Use case
Useful when you want to process or remove the first element.
let emotions = ["π", "π±", "π"];
let firstEmotion = emotions.shift();
console.log(emotions); // ["π±", "π"]
console.log(firstEmotion); // "π"
In this example, the first value "π" is removed from the array.
3. unshift()
The unshift() method adds one or more elements at the beginning of an array.
What it does
Adds items to the start of the array.
Use case
Useful when you want to prepend new values.
let emotions = ["π±", "π"];
emotions.unshift("π");
console.log(emotions); // ["π", "π±", "π"]
Here, "π" is added to the beginning of the array.
4. splice()
The splice() method is used to add, remove, or replace elements in an array.
What it does
Modifies the original array directly.
Use case
Useful when you need to update array items by position.
Syntax:
array.splice(startIndex, deleteCount, item1, item2, ...)
let emotions = ["π", "π±", "π‘"];
emotions.splice(1, 1, "π");
console.log(emotions); // ["π", "π", "π‘"]
In this example:
- 1 means start from index 1
- 1 means remove 1 element
- "π" is the new element to insert
Important: splice() changes the original array, so use it carefully.
5. map()
The map() method is used to transform every element of an array and create a new array.
What it does
Runs a function on each item and returns a new array.
Use case
Useful when you want to modify all items without changing the original array.
let emotions = ["π", "π±", "π‘"];
let updatedEmotions = emotions.map((emotion) => emotion + "β¨");
console.log(updatedEmotions); // ["πβ¨", "π±β¨", "π‘β¨"]
console.log(emotions); // ["π", "π±", "π‘"]
The original array stays the same, and map() gives back a new transformed array.
6. forEach()
The forEach() method executes a function once for each array element. It is mainly used for looping through an array.
What it does
Loops through all array items one by one.
Use case
Useful when you want to perform an action on each element.
let emotions = ["π", "π±", "π‘"];
emotions.forEach((emotion) => {
console.log(emotion);
});
// Output:
// π
// π±
// π‘
One important point is that forEach() does not return a new array like map(). It is mostly used when you only want to run some logic on each item.
Difference Between map() and forEach()
| Method | Returns New Array? | Main Purpose |
|---|---|---|
| map() | Yes | Transform array items |
| forEach() | No | Run logic for each item |
Quick Summary
- pop() removes the last element.
- shift() removes the first element.
- unshift() adds elements at the beginning.
- splice() adds, removes, or replaces elements.
- map() creates a new transformed array.
- forEach() loops through an array without returning a new array.
More In Javascript
Explore more Javascript articles
Read more articles from the same category or open the full category archive directly.
Javascript
JavaScript ES6+ Interview Questions & Concepts (Destructuring, Spread, Rest)
Javascript
JavaScript Basics Explained: Variables, Data Types & Functions with Examples
Javascript
JavaScript Arrays Explained with Examples Part 2 (Complete Beginner Guide)
Recent Posts
Bharat Ratna Award Winners List 1954 to 2024: History, Facts and Full List
26 March 2026
Nobel Prize 2025 Winners, History, Categories, Indian Winners & Important Facts (Hindi + English)
26 March 2026
JavaScript ES6+ Interview Questions & Concepts (Destructuring, Spread, Rest)
21 March 2026
JavaScript Basics Explained: Variables, Data Types & Functions with Examples
21 March 2026
JavaScript Arrays Explained with Examples Part 2 (Complete Beginner Guide)
20 March 2026
JavaScript Arrays Explained with Examples (Complete Beginner Guide)
20 March 2026
Callback vs Promises in JavaScript
20 March 2026
Async/Await Error Handling in JavaScript
20 March 2026
Javascript Array Methods Reduce Filter Includes Find findindex Sort
17 March 2026
Core React Interview Questions Every Developer Should Know
16 March 2026