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 t…


This content originally appeared on DEV Community and was authored by Root Lindow

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();

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();

output: false


This content originally appeared on DEV Community and was authored by Root Lindow


Print Share Comment Cite Upload Translate Updates
APA

Root Lindow | Sciencx (2022-01-28T15:52:00+00:00) every() and some() methods in JavaScript. Retrieved from https://www.scien.cx/2022/01/28/every-and-some-methods-in-javascript/

MLA
" » every() and some() methods in JavaScript." Root Lindow | Sciencx - Friday January 28, 2022, https://www.scien.cx/2022/01/28/every-and-some-methods-in-javascript/
HARVARD
Root Lindow | Sciencx Friday January 28, 2022 » every() and some() methods in JavaScript., viewed ,<https://www.scien.cx/2022/01/28/every-and-some-methods-in-javascript/>
VANCOUVER
Root Lindow | Sciencx - » every() and some() methods in JavaScript. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/01/28/every-and-some-methods-in-javascript/
CHICAGO
" » every() and some() methods in JavaScript." Root Lindow | Sciencx - Accessed . https://www.scien.cx/2022/01/28/every-and-some-methods-in-javascript/
IEEE
" » every() and some() methods in JavaScript." Root Lindow | Sciencx [Online]. Available: https://www.scien.cx/2022/01/28/every-and-some-methods-in-javascript/. [Accessed: ]
rf:citation
» every() and some() methods in JavaScript | Root Lindow | Sciencx | https://www.scien.cx/2022/01/28/every-and-some-methods-in-javascript/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.