Back to JavaScript
2026-01-045 min read

JavaScript Array Methods

Learn JavaScript Array Methods step by step with clear examples and exercises.

Why This Matters

Welcome to this full guide on JavaScript Array Methods! In this tutorial, we'll delve into the essential array methods every JavaScript developer should know. We'll cover why these methods matter, their prerequisites, and provide a worked example, common mistakes, practice questions, and FAQs.

Why This Matters

JavaScript array methods are crucial for manipulating and working with data efficiently in your projects. They save time by providing pre-built functions for various operations like searching, sorting, filtering, and modifying arrays. Mastering these methods is essential for acing interviews, debugging real-world issues, and creating robust applications.

Prerequisites

To fully grasp this tutorial, you should have a good understanding of the following:

  • Basic JavaScript syntax (variables, data types, operators)
  • Creating and accessing arrays in JavaScript
  • Understanding the difference between primitive and reference types in JavaScript

Core Concept

Creating an Array

First, let's create a simple array:

let fruits = ["apple", "banana", "orange"];

Common Array Methods

1. length property

The length property returns the number of elements in an array.

console.log(fruits.length); // Output: 3

2. push() method

The push() method adds one or more elements to the end of an array and returns the new length.

fruits.push("grape");
console.log(fruits); // Output: ["apple", "banana", "orange", "grape"]

3. pop() method

The pop() method removes the last element from an array and returns that value.

let removedFruit = fruits.pop();
console.log(removedFruit); // Output: "grape"
console.log(fruits); // Output: ["apple", "banana", "orange"]

4. shift() method

The shift() method removes the first element from an array and returns that value.

let removedFruit = fruits.shift();
console.log(removedFruit); // Output: "apple"
console.log(fruits); // Output: ["banana", "orange"]

5. unshift() method

The unshift() method adds one or more elements to the beginning of an array and returns the new length.

fruits.unshift("mango");
console.log(fruits); // Output: ["mango", "banana", "orange"]

6. splice() method

The splice() method changes the content of an array by removing, replacing, or adding elements. It takes up to four arguments: start (optional), count (optional), item1, ..., itemX.

fruits.splice(1, 0, "kiwi");
console.log(fruits); // Output: ["mango", "kiwi", "banana", "orange"]

7. slice() method

The slice() method returns a shallow copy of a portion of an array within the specified range. It takes two arguments: start (inclusive) and end (exclusive).

let slicedFruits = fruits.slice(1, 3);
console.log(slicedFruits); // Output: ["kiwi", "banana"]

8. concat() method

The concat() method combines two or more arrays and returns a new array. It takes zero or more arguments, each of which can be an array or a value.

let vegetables = ["carrot", "potato"];
let combinedArray = fruits.concat(vegetables);
console.log(combinedArray); // Output: ["mango", "kiwi", "banana", "orange", "carrot", "potato"]

9. indexOf() method

The indexOf() method returns the index of the first occurrence of a specified value within an array. If no match is found, it returns -1.

console.log(fruits.indexOf("kiwi")); // Output: 1
console.log(fruits.indexOf("avocado")); // Output: -1

10. includes() method

The includes() method determines whether an array includes a specified value, returning true or false. It is case-sensitive.

console.log(fruits.includes("kiwi")); // Output: true
console.log(fruits.includes("avocado")); // Output: false

11. forEach() method

The forEach() method executes a provided function once for each element in an array, but does not modify the original array.

fruits.forEach(function(fruit) {
console.log(fruit);
});
// Output: mango, kiwi, banana, orange

Worked Example

Let's create an example where we use these array methods to manage a shopping list:

let shoppingList = ["milk", "eggs", "bread"];
console.log("Original Shopping List:", shoppingList);

shoppingList.unshift("butter"); // Add butter to the beginning of the list
console.log("Shopping List After Adding Butter:", shoppingList);

let removedItem = shoppingList.pop(); // Remove the last item from the list (bread)
console.log("Shopping List After Removing Bread:", shoppingList);

shoppingList.splice(1, 0, "juice", "toothpaste"); // Add juice and toothpaste at index 1
console.log("Shopping List After Adding Juice and Toothpaste:", shoppingList);

let itemIndex = shoppingList.indexOf("juice"); // Find the index of juice
console.log("Index of Juice:", itemIndex);

Common Mistakes

1. Forgetting to return a value from a function that modifies an array

When writing functions that modify arrays, remember to return the modified array so it can be assigned back to the original variable.

function addItem(array, item) {
array.push(item);
return array; // Don't forget this!
}

let myArray = [1, 2, 3];
myArray = addItem(myArray, 4);
console.log(myArray); // Output: [1, 2, 3, 4]

2. Using == instead of === for strict comparison

Always use the strict equality operator (===) to compare values and avoid unexpected results due to type coercion.

let myArray = [1];
console.log(myArray == 1); // Output: true (but this is misleading)
console.log(myArray === 1); // Output: false

3. Not understanding the difference between slice() and splice()

While both methods modify arrays, slice() returns a new array without modifying the original one, while splice() modifies the original array and can also add or remove elements.

let myArray = [1, 2, 3];
let slicedArray = myArray.slice(1, 2); // Returns a new array: [2]
console.log(slicedArray);

myArray.splice(1, 1); // Modifies the original array and removes one element at index 1
console.log(myArray); // Output: [1, 3]

Practice Questions

  1. Write a JavaScript function that takes an array of numbers and returns a new array with only the even numbers.
  2. Given an array of strings, write a function that sorts the array in alphabetical order (case-insensitive).
  3. Create a JavaScript function that removes all duplicate values from an array.
  4. Write a JavaScript function that reverses the order of elements in an array.
  5. Given an array of objects representing students and their scores, write a function that returns the names of students who scored above 80.

FAQ

Q: What is the difference between push() and unshift()?

A: Both methods add elements to an array, but push() adds elements at the end of the array while unshift() adds elements at the beginning.

Q: Can I use array methods on non-array objects in JavaScript?

A: No, array methods can only be used on actual arrays (objects with a numeric length property and an indexed structure).

Q: What is the time complexity of most JavaScript array methods?

A: Most array methods have a time complexity of O(n), where n is the number of elements in the array. However, some methods like indexOf(), includes(), and find() can have an average-case time complexity of O(log n) if implemented efficiently.

JavaScript Array Methods | JavaScript | XQA Learn