Skip to main content

Command Palette

Search for a command to run...

Array Methods

Published
4 min read

Array.prototype.map()

Purpose:
Iterate through array elements and call a callback function for each element and store the result in a new modified array.

Syntax: map(callbackFn)

Callback Function parameters:
element,index,array

Return value:
Returns a new modified array.

Mutates original array:
No

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

Array.prototype.filter()

Purpose:
Iterate through array elements and return a new array containing only those elements for which the callback function returns true.

Syntax :- filter(callbackFn)

Callback Function parameters:

element,index,array

Return value:
Returns a new filtered array.

Mutates original array:
No

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

Array.prototype.reduce()

Purpose:
Iterate through array elements and reduce them to a single value (number, object, array, etc.).

Syntax :- reduce(callbackFn), reduce(callbackFn, initialValue)

Callback parameters:
accumulator(store result) ,currentValue (current element),index (index of current element),array

Return value:
Returns a single accumulated value.

Mutates original array:
No

Point to Remember :- If no initial value is given , the first element in the array is used as accumulator and If array is empty then it give the Error.

const numbers = [1, 2, 3, 4];
const sum = numbers.reduce((acc, curr) => acc + curr, 0);
console.log(sum); // 10

Array.prototype.every()

Purpose:
Checks whether all elements in the array satisfy the given condition.

Syntax:- every(callbackFn)

Callback Function parameters:
element,index,array

Return value:
Returns true if all elements pass the test, otherwise false.

Mutates original array:
No

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

Array.prototype.some()

Purpose:
Checks whether at least one element in the array satisfies the given condition.

Syntax:-some(callbackFn)

Callback parameters:
element,index,array

Return value:
Returns true if any element passes the test, otherwise false.

Mutates original array:
No

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

Array.prototype.find()

Purpose:
Returns the first element that satisfies the given condition.

Syntax:- find(callbackFn)

Callback parameters:
element,index,array

Return value:
Returns the matched element or undefined.

Mutates original array:
No

const numbers = [1, 4, 6, 8];
console.log(numbers.find(num => num > 5)); // 6
console.log(numbers.find(num => num < 5)); // 1

Array.prototype.findIndex()

Purpose:
Returns the index of the first element that satisfies the given condition.

Syntax :- findIndex(callbackFn)

Callback parameters:
element,index,array

Return value:
Returns the index of the matched element or -1.

Mutates original array:
No

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

Array.prototype.flat()

Purpose:
Flattens nested arrays into a single-level array.

Syntax:- flat(depth)

Parameters:
depth (optional, default = 1)

Return value:
Returns a new flattened array.

Mutates original array:
No

const nested = [1, [2, [3, [4]]]];
console.log(nested.flat(2)); // [1, 2, 3, [4]]

Array.prototype.forEach()

Purpose:
Iterate through array elements and execute a callback function for each element.

Syntax:- forEach(callbackFn)

Callback Function parameters:
element,index,array

Return value:
Does not return anything (undefined).

Mutates original array:
No

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

Array.prototype.shift()

Purpose:
Removes the first element from the array.

Parameters:
None

Return value:
Returns the removed element.

Mutates original array:
Yes

 const arr = [1,2,3];
 const firstElement = array.shift();
 console.log(firstElement); //1

Array.prototype.unshift()

Purpose:
Adds one or more elements to the beginning of the array.

Parameters:
element1,element2…

Return value:
Returns the new length of the array.

Mutates original array:
Yes

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

Array.prototype.reverse()

Purpose:
Reverses the order of elements in the array.

Parameters:
None

Return value:
Returns the reversed array.

Mutates original array:
Yes

const array = ["one","two","three"]
console.log(array.reverse());

Array.prototype.includes()

Purpose:
Checks whether the array contains a specified element.

Syntax:-includes(searchElement, fromIndex)

Parameters:
searchElement, fromIndex(optional)

Return value:
Returns true if element exists, otherwise false.

Mutates original array:
No

const array = [1,2,3];
console.log(array.includes(2));

Array.prototype.slice()

Purpose:
Extracts a portion of an array and returns it as a new array.

Syntax :- array.slice(startIndex,endIndex) where startIndex is included and endIndex is excluded and also support negative index

Returns value :-
Returns a new array.

Mutates original Array :-

No

const arr = [1, 2, 3, 4, 5];

arr.slice(1, 4);// [2, 3, 4]
arr.slice(2); // [3, 4, 5]
arr.slice(-3); // [3, 4, 5]
arr.slice(); // [1, 2, 3, 4, 5]

Array.prototype.splice()

Purpose:
Adds, removes, or replaces elements in the original array.

Syntax:
array.splice(start, deleteCount, item1, item2, ...)

Return value:
Returns an array of removed elements.

Mutates original array:
Yes

//Removing the Elements
const num = [1, 2, 3, 4, 5];
num.splice(1, 2);
console.log(num); // [1, 4, 5]

//Add Elements
const num2 = [1,2,3];
num2.splice(1,0,4)
console.log(arr); // [1, 4, 2, 3]

//Replace Elements
const arr = [1,2,3]
arr.splice(1,1,5)
console.log(arr); // [1,5,3]