DEV Community

Root Lindow
Root Lindow

Posted on

every() and some() methods in JavaScript

The two methods can be used in an array

The every() method is used to determine whether or not all of the array's elements satisfy the given condition.

The some() method is used to determine whether at least one element of the array meets the supplied criterion.

The only difference is that the some() function returns true if any of the predicates are true, but the every() method returns true if all of them are true.

The some() method is used in this example.

function isnumbereven(element) {
    return (element % 2 == 0);
}

function print() {
    var arr = [ 4, 3, 13, 43, 58 ];

    // cheking if at least one number is even
    var value = arr.some(isnumbereven);
    console.log(value);
}
print();
Enter fullscreen mode Exit fullscreen mode

output: true

The every() method is used in this example.

function isnumbereven(element) {
    return (element % 2 == 0);
}

function print() {
    var arr = [ 4, 3, 13, 43, 58 ];

    // cheking if all the number are even
    var value = arr.every(isnumbereven);
    console.log(value);
}
print();
Enter fullscreen mode Exit fullscreen mode

output: false

Top comments (1)

Collapse
 
marynarzswiata profile image
MarynarzSwiata.pl

is very useful often