Array Methods Every JS Developer should know

Rakesh Kumar Shaw
2 min readAug 3, 2021

--

Array Map ()

The map() method creates a new array with the results of calling a function for every array element. It calls the provided function once for each element in an array, in order. Map() does not execute the function for empty elements and does not change the original array.

// Returns an array of with square root of all the elements in the arrayconst numbers = [4, 9, 16, 25];const newArr = numbers.map(Math.sqrt); console.log(newArr);  // [2,3,4,5]

Array Filter()

The filter() method creates an array filled with all array elements that pass a test (provided by a function) and does not execute the function for empty array elements. It does not change the original array.

// Returns an array of all the words with length more than 6const words = ['spray', 'limit', 'elite', 'exuberant', 'destruction', 'present'];const result = words.filter(word => word.length > 6);console.log(result); // ["exuberant", "destruction", "present"]

Array Some()

The some() method checks if any of the elements in an array pass a test (provided as a function). It executes the function once for each element in the array:

  • If it finds an array element where the function returns a true value, some() returns true (and does not check the remaining values)
  • Otherwise, it returns false

some() does not execute the function for empty array elements and do not change the original array.

//Checks whether there is atleast one even number and returns a booleanconst array = [1, 2, 3, 4, 5];const even = (element) => element % 2 === 0;console.log(array.some(even)); // expected output: true

Array Every()

The every() method returns true if all elements in an array pass a test (provided as a function). The method executes the function once for each element present in the array:

  • If it finds an array element where the function returns a false value, every() returns false (and does not check the remaining values)
  • If no false occur, every() returns true

every() does not execute the function for empty array elements and does not change the original array.

//Checks whether all the elements pass the given condition and returns a booleanconst array1 = [1, 30, 39, 29, 10, 13];console.log(array1.every(currentValue => currentValue < 40); // true

Array forEach()

The forEach() method calls a function once for each element in an array, in order and is not executed for array elements without values.

//Iterates over every element of the arrayconst array1 = ['a', 'b', 'c'];array1.forEach(element => console.log(element));

Hope this may be a helpful article for the interviews. All the best !!

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

Rakesh Kumar Shaw
Rakesh Kumar Shaw

No responses yet

Write a response