Iterate an array

August 13, 2025 - Reading time: 5 minutes

Here are some notes from the FreeCodeCamp lectures on some higher order functions to iterate through arrays in JavaScript.

Search an array

filter

This methods calls a function for every array item and add the item into a new array if the test returns true.

Structure

array.filter((item, index, array) => // verify if a condition is true);

Example

const originalArray = [1, 2, 3, 4, 5];
const newArray = originalArray.filter(item => item > 2); 
console.log(originalArray); // [1, 2, 3, 4, 5]
console.log(newArray); // [3, 4, 5]

some / every

This methods calls a function for every array item and returns true or false depending if a condition is met:

  • some: on at least 1 item
  • every: on all items

Structure

array.some((item, index, array) => // verify if a condition is true);
array.every((item, index, array) => // verify if a condition is true);

Example

[1, 2, 3, 4, 5].some(item => item > 3); //  true
[1, 2, 3, 4, 5].every(item => item > 3); //  false

Modify an array

map

This methods calls a function for every array item and returns a new array.

Structure

array.map((item, index, array) => // do something);

Example

const originalArray = [1, 2, 3, 4, 5];
const newArray = originalArray.map((item) => item * 2);
console.log(originalArray); // [1, 2, 3, 4, 5]
console.log(newArray); // [2, 4, 6, 8, 10]

forEach

This methods calls a function for every array item and changes the original array. It returns undefined.

Structure

array.forEach((item, index, array) => // do something);

Example

const originalArray = [1, 2, 3, 4, 5];
originalArray.forEach((item, index, arr) => arr[index] = item * 2);
console.log(originalArray); // [2, 4, 6, 8, 10]

sort

This method sorts the elements of an array and returns the sorted array.

["World", "Hello"].sort(); // ["Hello", "World"]

If you have to work with numbers, you should pass a compare function (the sort method converts items into strings and checks the UTF-16 code units values). 

[4, 2, 1, 5, 3].sort((a, b) => a - b); // [1, 2, 3, 4, 5]

The parameters a and b are the two elements being compared. The compare function should return a negative value if a should come before b, a positive value if a should come after b, and zero if a and b are equal.

randomize an array of items

The sort method can be used to randomize an array of items. Here's an example from FreeCodeCamp:

One way to randomize an array of items would be to subtract 0.5 from Math.random() which produces random values that are either positive or negative. This makes the comparison result a mix of positive and negative values, leading to a random ordering of elements.

const names = ["Tom", "Jessica", "Quincy", "Naomi"];
names.sort(() => Math.random() - 0.5); // result could be [ "Jessica", "Naomi", "Quincy", "Tom" ]

reduce

This methods condenses a given array into a single value. It returns a value and does not modify the original array.

Structure

array.reduce(
  (accumulator, currentValue) => accumulator + currentValue,
  initialValue // optional, default is array[0]
)

Example

[1, 2, 3, 4, 5].reduce((acc, cur) => acc + cur, 1); // 16 (1 + 1 = 2; 2 + 2 = 4; 4 + 3 = 7; 7 + 4 = 11; 11 + 5 = 16)