some vs every vs foreach cover image

JavaScript Array – some() vs every() vs forEach()

This article explains the array’s some(), every() and the forEach() method.

The some() method

The some() method iterates through elements and checks if any value in the array satisfies a condition. The some() method accepts a boolean expression with the following signature:

(element) => boolean

where the element is the current element in the array whose value is being checked

boolean denotes that the function returns a boolean value

Consider the following array of integers:

let arr = [1, 2, 3, 4, 5, 6, 7, 8];

We will be using the some() method to check if at least one element is even in the array.

arr.some((value)=> {
    return (value%2 == 0);
});

Output:

true

As the array contains elements divisible by 2, so the some() method returned true.

When tried some() method with the logic to find negative numbers

arr.some((value)=> {
    return (value < 0);
});

The output is

false

As there are no numbers in the array that are negative.

 

The some() method stops iterating as soon as the element is found which satisfies the required boolean expression.

let index = 0;
arr.some((value) => {
    index++;
    console.log(index);
    return (value % 2 == 0);
});

Output is:

1
2
true

 

The every() method

Opposing to some(), the every() method checks if each of the element in the array satisfies the boolean expression. If even a single value doesn’t satisfy the element it returns false, else it returns true.

let arr = [1, 2, 3, 4, 5, 6, 7, 8];
arr.every((value)=> {
    return (value > 0);
});

Since all of the values in the array arr are positive, so the boolean expression satisfies for all of the values. We receive true as output.

With even a single value, not satisfying the boolean expression, we get the boolean output as false.

arr.every((value)=> {
    return (value == 5);
});

Output:

false

 

The every() method stops iterating over the elements as soon as any value fails the boolean expression.

let index = 0;
arr.every((value) => {
    index++;
    console.log(index);
    return (value != 4);
});

Output:

1
2
3
false

 

The forEach() method

The forEach() method, as the name suggests, is used to iterate over every element of the array and perform some desired operation with it.

let arr = [1, 2, 3, 4, 5, 6, 7, 8];
arr.forEach((value) => {
    console.log(value == 5);
});

We get the output as:

false
false
false
false
true
false
false
false

Nothing different here, just iterating each element with some operation (as required) on it.


One comment

  1. Very nice explained.
    Just you need to mention that forEach() couldn’t break. So either you could use some() with returning true when you want to break or use good old for() and finally you could try-catch but don’t look that nice up to me.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.