JS Array Methods
In this page:
array.push(item);
array.pop();
array.map(item => expression);
array.filter(item => condition);
array.slice(start, end);
Adding and Removing: push, pop, shift, unshift
push() adds an element to the end of an array, pop() removes the last element, unshift() adds an element to the beginning, and shift() removes the first element, all modifying the original array directly.
Example: Adding and Removing: push, pop, shift, unshift
const list = [2, 3];
list.push(4);
list.unshift(1);
console.log(list); // [1, 2, 3, 4]
list.pop();
list.shift();
console.log(list); // [2, 3]
splice, slice, concat, and join
splice() adds or removes elements at any position and modifies the array in place, slice() extracts a portion without modifying the original, concat() combines arrays into a new one, and join() converts an array into a single string.
Example: splice, slice, concat, and join
const nums = [1, 2, 3, 4];
console.log(nums.slice(1, 3)); // [2, 3], original untouched
nums.splice(1, 1, "x");
console.log(nums); // [1, "x", 3, 4]
console.log([1, 2].concat([3, 4]));
console.log(nums.join("-"));
indexOf and find
indexOf() returns the position of the first matching element or -1 if it's not found, while find() returns the first element that satisfies a given test function, or undefined if none match.
Example: indexOf and find
// Declare the constant `nums`, set to `[10, 20, 30]`
// Declare the constant `nums`, set to `[10, 20, 30]`
const nums = [10, 20, 30];
// Print `nums.indexOf(20)` to the console
// Print `nums.indexOf(20)` to the console
console.log(nums.indexOf(20));
// Print `nums.find(n => n > 15)` to the console
// Print `nums.find(n => n > 15)` to the console
console.log(nums.find(n => n > 15));
filter and map
filter() creates a new array containing only elements that pass a given test, while map() creates a new array where every element has been transformed by a given function, both leave the original array untouched.
Example: filter and map
// Declare the constant `nums`, set to `[1, 2, 3, 4, 5]`
// Declare the constant `nums`, set to `[1, 2, 3, 4, 5]`
const nums = [1, 2, 3, 4, 5];
// Print `nums.filter(n => n % 2 === 0)` to the console
// Print `nums.filter(n => n % 2 === 0)` to the console
console.log(nums.filter(n => n % 2 === 0));
// Print `nums.map(n => n * 2)` to the console
// Print `nums.map(n => n * 2)` to the console
console.log(nums.map(n => n * 2));
reduce, forEach, and sort
reduce() combines every element of an array into a single accumulated value, forEach() runs a function once for each element purely for side effects without returning anything useful, and sort() reorders an array's elements, by default treating them as strings unless given a custom compare function.
Example: reduce, forEach, and sort
// Declare the constant `nums`, set to `[4, 2, 7]`
// Declare the constant `nums`, set to `[4, 2, 7]`
const nums = [4, 2, 7];
// Print `nums.reduce((sum, n) => sum + n, 0)` to the console
// Print `nums.reduce((sum, n) => sum + n, 0)` to the console
console.log(nums.reduce((sum, n) => sum + n, 0));
// Call `nums.forEach(n => console.log(n))`
// Call `nums.forEach(n => console.log(n))`
nums.forEach(n => console.log(n));
// Print `[...nums].sort((a, b) => a - b)` to the console
// Print `[...nums].sort((a, b) => a - b)` to the console
console.log([...nums].sort((a, b) => a - b));
- Confusing map(), which returns a new transformed array, with forEach(), which returns nothing and is used only for side effects.
- Forgetting that filter() and map() return new arrays, leaving the original array completely unchanged.
- Using push() and expecting a new array back, push() modifies the array in place and returns the new length, not the array itself.
- push, pop, shift, and unshift add or remove elements from the beginning or end of an array.
- filter, find, and map create new arrays or values based on a condition or transformation applied to each element.
- reduce combines every element into a single value, and forEach runs a function once for each element without returning anything.
All standard array methods covered here are supported in every modern browser and are considered a stable, foundational part of JavaScript.
Chapter Quiz — Complete all 6 topics to unlock
0/6 topics done
Complete these topics first: