DEV Community

Cover image for Write Cleaner JavaScript Code With .some() And .every() Array Methods
Marc Backes
Marc Backes

Posted on • Originally published at marc.dev

Write Cleaner JavaScript Code With .some() And .every() Array Methods

Writing clean and readable code is important. Especially, but not only when working with others. In this article, I'll teach you how to use the lesser known array methods .some() and .every() to write nice, readable code in your next project.

How do they work?

Both functions work in a similar way. What they do is, they iterate over every item of an array and check if a function (defined by the developer) returns true in some or in every item.

Great use cases for this are situations where you need to check if something is true (or false) for a given array.

Examples

Let's assume the following array:

const people = [
    { name: 'John', age: 47 },
    { name: 'Martha', age: 89 },
    { name: 'Edward', age: 31 },
    { name: 'Michele', age: 18 }
];
Enter fullscreen mode Exit fullscreen mode

Every

If we wanted to find out if all of the people in the array are above legal age (>= 18 years), we could write the following code with forEach:

let allAboveAge = true // assume they all are above age
people.forEach(person => { // iterate over every item
    if(person.age < 18) { // check if below age
        allAboveAge = false // at least one is not above age
    }
})
Enter fullscreen mode Exit fullscreen mode

The code above could be hard to understand at first, because it's not easily readable for another developer. They first have to get behind the logic of what you're trying to do.

To achieve the same thing with .every(), you can do it in one easy-to-read line of code:

const allAboveAge = people.every(person => person.age >= 18)
Enter fullscreen mode Exit fullscreen mode

The function you pass to the .every()-method is should give as a result the thing that needs to be true for each and every item in the array. (In this example, person is the item being iterated at a given moment)

Some

.some() works very similar to .every(). The only difference is, that instead of checking that for every item the statement is true, it only checks if for some (at least one!) items it's true.

So, if you were to check if the array contains at least one person that is above age, you can do it with the following statement, using the .some() method:

const oneOrMoreAboveAge = people.some(person => person.age >= 18)
Enter fullscreen mode Exit fullscreen mode

Summary

As you can see, both methods work beautifully similar in a way you just need to exchange the method called, and the rest can stay the same.

Those two methods are a handy little tool if you want to test the array for something that has Boolean as a result.

Try to use it in your next project where it applies, or see if you find a way to refactor code in your current project where this way results in a much nicer and more readable code.

Photo by Frank Vessia on Unsplash

Top comments (10)

Collapse
 
aritdeveloper profile image
Arit Developer

Excellent post - short and sweet! 🍬

In your .some() example, perhaps changing the variable name to something more appropriate? Since .some() requires only one comparison to return true:

const oneOrMoreAboveAge = people.some(person => person.age >= 18)
Collapse
 
themarcba profile image
Marc Backes

You’re completely right. Fixed it πŸ‘

Collapse
 
thepeoplesbourgeois profile image
Josh • Edited

Nice write-up! There's an added benefit to using some and every over the forEach loop in that some stops iterating as soon as it finds a true value, and every stops as soon as it finds a false 😊

Collapse
 
metalmikester profile image
Michel Renaud

Thanks. I tend to forget about these (still technically new to JavaScript). Unlike a few other constructs, these are also pretty clear as to their intent.

Collapse
 
waylonwalker profile image
Waylon Walker

Thanks for sharing! I would have never thought to use these before and would have likely done something with filter.

πŸ‘Œ Very nice website BTW. It is super well designed!

Collapse
 
themarcba profile image
Marc Backes

Thank you 😊

Collapse
 
juditlehoczki profile image
Judit Lehoczki (she/her)

Very useful for us newbies. Thank you. β˜ΊοΈπŸ™

Collapse
 
twostepdeveloper profile image
twostepdeveloper

cool

Collapse
 
iamsbharti profile image
Iamsbharti

You even declared the chair used ... :)
By the way its cool!!!

Some comments may only be visible to logged-in visitors. Sign in to view all comments.