JavaScript Array Methods

Different types of JavaScript Array Methods
 

push()

Adds one or more elements to the end of an array and returns the new length of the array.

const numbers = [1,2,3];
numbers.push(4,5);
console.log(numbers);
//[1,2,3,4,5]

Push

pop()

Removes the last element from an array and returns that element.

const numbers = [1,2,3,4,5];
const lastNumber = numbers.pop();
console.log(lastNumber);
// 5

Pop

shift()

Removes the first element from an array and returns that element.

const numbers = [1,2,3,4,5];
const firstNumber = numbers.shift();
console.log(firstNumber);
// 1

Shift

unshift()

Adds one or more elements to the beginning of an array and returns the new length of the array.

const numbers = [1,2,3,4];
numbers.unshift(0, -1);
console.log(numbers);
 // 0, -1, 1, 2, 3, 4

unshift

find()

Returns the value of the first element in the array that satisfies the provided testing function. Otherwise, undefined is returned.

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

Find

some()

Tests whether at least one element in the array passes the test implemented by the provided function. It returns true if any element passes the test, otherwise, it returns false.

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

Some

every()

Tests whether all elements in the array pass the test implemented by the provided function. It returns true if all elements pass the test, otherwise, it returns false.

const numbers = [1,2,3,4,5];
const allEvenNumbers = numbers.every((num) => num % 2 === 0);
console.log(allEvenNumbers);
// false

Every

sort()

Sorts the elements of an array in place and returns the sorted array. The default sort order is built upon converting the elements into strings, then comparing their sequences of UTF-16 code units values.

const fruits = ['banana', 'apple', 'orange', 'grape'];
fruits.sort();
console.log(fruits);
// ['apple', 'banana', 'grape', 'orange']

Sort

const numbers = [100, 20, 200, 30];
numbers.sort((a, b) => a - b);
console.log(numbers);
// [20, 30, 100, 200]

Sort Number

includes()

Determines whether an array includes a certain element, returning true or false as appropriate.

const numbers = [1, 2, 3, 4, 5];
const includesFour = numbers.includes(4);
console.log(includesFour);
// true

Includes

slice()

Returns a shallow copy of a portion of an array into a new array object selected from start to end (end not included). The original array will not be modified.

const numbers = [1, 2, 3, 4, 5];
const slicedNumbers = numbers.slice(0,3);
console.log(slicedNumbers);
// [1, 2, 3]

Slice

map()

Creates a new array with the results of calling a provided function on every element in the calling array.

const numbers = [1, 2, 3, 4];
const doubledNumbers = numbers.map((num) => num * 2 );
console.log(doubledNumbers);
// [2, 4, 6, 8]

Map

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((num) => num % 2 === 0 );
console.log(evenNumbers);
// [2, 4]

Filter

reduce()

Executes a reducer function on each array element, resulting in a single output value.

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

Reduce

forEach()

Executes a provided function once for each array element

const numbers = [1, 2, 3, 4, 5];
numbers.forEach((num) => console.log(num * 2));
// [2, 4, 6, 8, 10]

forEach

indexOf()

Returns the first index at which a given element can be found in the array, or -1 if it is not present.

const fruits = ['banana', 'apple', 'orange', 'grape'];
const orangeIndex = fruits.indexOf('orange');
console.log(orangeIndex);
// 2

IndexOf

lastIndexOf()

Returns the last index at which a given element can be found in the array, or -1 if it is not present.

const fruits = ['banana', 'apple', 'orange', 'grape', 'orange'];
const orangeIndex = fruits.lastIndexOf('orange');
console.log(orangeIndex);
// 4

Last Index Of

reverse()

Reverses the order of the elements of an array in place. The first element becomes the last, and the last becomes the first.

const numbers = [1, 2, 3, 4, 5];
numbers.reverse();
console.log(numbers);
// [5, 4, 3, 2, 1]

Reverse

concat()

Returns a new array that includes elements from the original array and additional elements.

const numbers = [1, 2, 3];
const moreNumbers = [4, 5];
const allNumbers = numbers.concat(moreNumbers);	
console.log(allNumbers);
// [1, 2, 3, 4, 5]

Concat

join()

Joins all elements of an array into a string. The elements are separated by a specified separator string.

const fruits = ['banana', 'apple', 'orange', 'grape'];
const joinedFruits = fruits.join(', ');
console.log(joinedFruits);
// 'banana, apple, orange, grape'

Join

toString()

Returns a string representing the specified number or array and its elements.

const numbers = [1, 2, 3];
const numberString = numbers.toString();	
console.log(numberString);
// '1, 2, 3'

toString()