DEV Community

Root Lindow
Root Lindow

Posted on • Edited on

3 2

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

Sentry image

Hands-on debugging session: instrument, monitor, and fix

Join Lazar for a hands-on session where you’ll build it, break it, debug it, and fix it. You’ll set up Sentry, track errors, use Session Replay and Tracing, and leverage some good ol’ AI to find and fix issues fast.

RSVP here →

Top comments (1)

Collapse
 
marynarzswiata profile image
MarynarzSwiata.pl

is very useful often

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay