JavaScript Array Methods

JavaScript arrays come with a wide range of built-in methods that make it easy to manipulate and interact with array elements. These methods help with adding, removing, searching, sorting, and iterating over elements.

1. Mutating Array Methods

These methods modify the original array.

push(): Adds one or more elements to the end of an array.

const fruits = ['Apple', 'Banana'];
fruits.push('Cherry');
console.log(fruits);  // Output: ['Apple', 'Banana', 'Cherry']

Try It Now

pop(): Removes the last element from an array.

const fruits = ['Apple', 'Banana', 'Cherry'];
fruits.pop();
console.log(fruits);  // Output: ['Apple', 'Banana']

Try It Now

shift(): Removes the first element from an array.

const fruits = ['Apple', 'Banana', 'Cherry'];
fruits.shift();
console.log(fruits);  // Output: ['Banana', 'Cherry']

Try It Now

unshift(): Adds one or more elements to the beginning of an array.

const fruits = ['Banana', 'Cherry'];
fruits.unshift('Apple');
console.log(fruits);  // Output: ['Apple', 'Banana', 'Cherry']

Try It Now

splice(): Adds or removes elements from an array.

const fruits = ['Apple', 'Banana', 'Cherry'];
fruits.splice(1, 1, 'Blueberry');  // Removes 'Banana' and adds 'Blueberry'
console.log(fruits);  // Output: ['Apple', 'Blueberry', 'Cherry']

Try It Now

2. Non-Mutating Array Methods

These methods return a new array or value and do not modify the original array.

slice(): Returns a shallow copy of a portion of an array.

const fruits = ['Apple', 'Banana', 'Cherry', 'Date'];
const citrus = fruits.slice(1, 3);
console.log(citrus);  // Output: ['Banana', 'Cherry']

Try It Now

concat(): Merges two or more arrays.

const arr1 = [1, 2];
const arr2 = [3, 4];
const result = arr1.concat(arr2);
console.log(result);  // Output: [1, 2, 3, 4]

Try It Now

map(): Creates a new array with the results of calling a function for every array element.

const numbers = [1, 2, 3];
const squared = numbers.map(x => x * x);
console.log(squared);  // Output: [1, 4, 9]

Try It Now

filter(): Creates a new array with all elements that pass the test implemented by the provided function.

const numbers = [1, 2, 3, 4, 5];
const evenNumbers = numbers.filter(x => x % 2 === 0);
console.log(evenNumbers);  // Output: [2, 4]

Try It Now

reduce(): Executes a reducer function on each element of the array, resulting in a single output value.

const numbers = [1, 2, 3, 4];
const sum = numbers.reduce((total, num) => total + num, 0);
console.log(sum);  // Output: 10

Try It Now

find(): Returns the value of the first element in the array that satisfies the provided testing function.

const numbers = [1, 2, 3, 4, 5];
const found = numbers.find(num => num > 3);
console.log(found);  // Output: 4

Try It Now

findIndex(): Returns the index of the first element in the array that satisfies the provided testing function.

const numbers = [1, 2, 3, 4, 5];
const index = numbers.findIndex(num => num > 3);
console.log(index);  // Output: 3

Try It Now

includes(): Determines whether an array includes a certain element.

const fruits = ['Apple', 'Banana', 'Cherry'];
console.log(fruits.includes('Banana'));  // Output: true

Try It Now

some(): Tests whether at least one element in the array passes the test implemented by the provided function.

const numbers = [1, 2, 3, 4];
const hasEven = numbers.some(num => num % 2 === 0);
console.log(hasEven);  // Output: true

Try It Now

every(): Tests whether all elements in the array pass the test implemented by the provided function.

const numbers = [2, 4, 6];
const allEven = numbers.every(num => num % 2 === 0);
console.log(allEven);  // Output: true

Try It Now

3. Iteration Methods

forEach(): Executes a provided function once for each array element.

const fruits = ['Apple', 'Banana', 'Cherry'];
fruits.forEach(fruit => console.log(fruit));
// Output:
// Apple
// Banana
// Cherry

Try It Now

keys(): Returns a new Array Iterator object that contains the keys for each index in the array.

const fruits = ['Apple', 'Banana', 'Cherry'];
const keys = fruits.keys();
for (let key of keys) {
    console.log(key);  // Output: 0, 1, 2
}

Try It Now

values(): Returns a new Array Iterator object that contains the values for each index in the array.

const fruits = ['Apple', 'Banana', 'Cherry'];
const values = fruits.values();
for (let value of values) {
    console.log(value);  // Output: Apple, Banana, Cherry
}

Try It Now

entries(): Returns a new Array Iterator object that contains the key/value pairs for each index in the array.

const fruits = ['Apple', 'Banana', 'Cherry'];
const entries = fruits.entries();
for (let [index, fruit] of entries) {
    console.log(index, fruit);  // Output: 0 'Apple', 1 'Banana', 2 'Cherry'
}

Try It Now

4. Sorting and Reversing

sort(): Sorts the elements of an array in place and returns the array.

const fruits = ['Banana', 'Apple', 'Cherry'];
fruits.sort();
console.log(fruits);  // Output: ['Apple', 'Banana', 'Cherry']

Try It Now

reverse(): Reverses the order of the elements in an array.

const fruits = ['Apple', 'Banana', 'Cherry'];
fruits.reverse();
console.log(fruits);  // Output: ['Cherry', 'Banana', 'Apple']

Try It Now

5. Joining and Splitting

join(): Joins all elements of an array into a string.

const fruits = ['Apple', 'Banana', 'Cherry'];
const result = fruits.join(', ');
console.log(result);  // Output: 'Apple, Banana, Cherry'

Try It Now

toString(): Returns a string representing the array and its elements.

const fruits = ['Apple', 'Banana', 'Cherry'];
console.log(fruits.toString());  // Output: 'Apple,Banana,Cherry'

Try It Now

Summary

  • Mutating Methods: Modify the original array (push(), pop(), splice(), etc.).
  • Non-Mutating Methods: Do not modify the original array and return new data (map(), filter(), reduce(), etc.).
  • Iteration Methods: Used to iterate over arrays (forEach(), map(), filter(), etc.).
  • Sorting and Reversing Methods: Used to sort or reverse arrays (sort(), reverse()).
  • Joining and Splitting Methods: Convert arrays to strings (join(), toString()).