DEV Community

Bishal Maharjan
Bishal Maharjan

Posted on

When SOME or EVERY things matters.

Ever since the arrow operators (=>) arrived in the javascript, Higher Order Functions (HOF) have gained a lot of popularity among the javascript developers. HOF operations such as forEach(), filter(), map(), reduce(), are the most common HOF among developers. These HOF are so much popular among developers such that they are adapted to many other programming languages as-well.

But do you know there are other functions released by ECMA as of 2015 which are still rarely used such as every() and some(), which are of great use.

Recently I came across a problem where I had to return TRUE value if there was a single value greater than 20 in an array (CONDITION) 🧐.

At a glance, what I thought was I need a FLAG 🏁 that will have a default value FALSE. Then, run the loop on array that checks the condition with every value and BREAK the loop when condition is matched.

let data = [ 1, 2, 3, 4, 5, 6, 22 ]
let flag = false

for(let i = 0; i <= data.length; i++){
    if(data[i] > 20){
        flag = true
        break
    }
}

if(flag){
    console.log('Above array contains numbers greater than 20.')
}else{
    console.log('Above array does not contain numbers greater than 20.')
}

\\********result**********
\\Above array contains numbers greater than 20.

It is readable but I thought what if I could make codes smaller, what if there was already a function for this in 'loadash' (which was already there in my project). But while researching about these functions I found out about these cool functions some() and every() which were already there as Array operations from way back.

So, the code above became:

let data = [1,2,3,4,5,6,22]
let flag = data.some( value => value > 20)

if(flag){
    console.log('Above array contains numbers greater than 20.')
}else{
    console.log('Above array does not contain numbers greater than 20.')
}

\\******** Result **********
\\Above array contains numbers greater than 20.

Woooooaaaaahh... it just blew my mind 🤯🤯🤯🤯

Then there was another function which had similar function, every().
While some() checks for any one value in the array to meet the condition, every() checks for all the value in the array to meet the condition.

let data = [ 22, 23, 3, 4, 5, 6]
let flag = data.every( value => value > 20)

if(flag){
    console.log('All the numbers in above array are greater than 20.')
}else{
    console.log('Not all the numbers in above array are greater than 20.')
}

//******* Result ***********
//Not all the numbers in above array are greater than 20.

Here are some reference to some() and every()

That's it for today, So Did you find this blog post helpful? 🤞

P.S. It is my first article on the web 🤓

Top comments (5)

Collapse
 
asimdahall profile image
Asim Dahal

Wow awesome

Collapse
 
bishal0622 profile image
Bishal Maharjan

Thanks bro

Collapse
 
asimdahall profile image
Asim Dahal

Welcome

Collapse
 
csangharsha profile image
Sangharsha Chaulagain

nice article in some and every HOF. Love it. Keep blogging.

Collapse
 
bishal0622 profile image
Bishal Maharjan

Thanks.. will keep on trying..✌️✌️