Here are some important JavaScript Array methods you need to know 👇🏼
-
map(callbackFn):-
mapapplies a provided function (callback) to each element in an array and returns a new array with the results. - Example:
const fruits = ['🍎', '🍌', '🍇', '🍒']; const result = fruits.map((fruit) => fruit + '🍔'); // Result: ['🍎🍔', '🍌🍔', '🍇🍔', '🍒🍔']In this example, the callback function concatenates '🍔' to each fruit.
-
-
filter(callbackFn):-
filtercreates a new array containing all elements that pass a provided test (callback). - Example:
const numbers = [1, 2, 3, 4, 5]; const result = numbers.filter((number) => number % 2 === 0); // Result: [2, 4]The callback function checks if a number is even and includes it in the result array.
-
-
find(callbackFn):-
findreturns the first element in the array that satisfies the provided test (callback). - Example:
const fruits = ['🍇', '🍇', '🍏', '🍏']; const result = fruits.find((fruit) => fruit === '🍏'); // Result: '🍏'The callback function searches for the first occurrence of '🍏' in the array.
-
-
findIndex(callbackFn):-
findIndexreturns the index of the first element in the array that satisfies the provided test (callback). - Example:
const fruits = ['🍇', '🍇', '🍇', '🍒']; const result = fruits.findIndex((fruit) => fruit === '🍒'); // Result: 3The callback function finds the index of the first '🍒' in the array.
-
-
fill(value, start):-
fillchanges all elements in an array to a static value (value) from a starting index (start). - Example:
const numbers = ['🍏','🍏','🍏','🍏']; numbers.fill('🍇',1); // Result: ['🍏', '🍇', '🍇', '🍇']It fills all elements from index 1 onward with the value 🍇.
-
-
some(callbackFn):-
somechecks if at least one element in the array satisfies the provided test (callback) and returns a Boolean. - Example:
const numbers = [1, 2, 3, 4, 5]; const result = numbers.some((number) => number > 3); // Result: trueThe callback function checks if at least one number is greater than 3.
-
-
every(callbackFn):-
everychecks if all elements in the array satisfy the provided test (callback) and returns a Boolean. - Example:
const numbers = [1, 2, 3, 4, 5]; const result = numbers.every((number) => number > 3); // Result: falseThe callback function checks if all numbers are greater than 3 (which is not true).
-
-
reduce(callbackFn, initialValue):-
reduceapplies a function (callback) against an accumulator and each element in the array to reduce it to a single value. - Example:
const numbers = [1, 2, 3, 4, 5]; const result = numbers.reduce((accumulator, number) => accumulator + number, 0); // Result: 15The callback function accumulates the sum of all numbers with an initial value of 0.
-
-
includes(searchElement):-
includeschecks if an array includes a certain element (searchElement) and returns a Boolean. - Example:
const colors = ['🟣', '🟣', '🟡', '🟡']; const result = colors.includes('🟣'); // Result: trueIt checks if '🟣' is present in the array.
-
-
indexOf(searchElement):-
indexOfreturns the first index at which a given element (searchElement) is found in the array, or -1 if it's not present. - Example:
const colors = ['🟣', '🟣', '🟡', '🟡']; const result = colors.indexOf('🟡'); // Result: 2It finds the first occurrence of '🟡' at index 2 in the array.
-
You find this blog helpful. Please like and share with others.
Top comments (0)