DEV Community

Kelvin Wangonya
Kelvin Wangonya

Posted on • Updated on • Originally published at wangonya.com

Javascript array iteration with 'some()' and 'every()'

If you're using an array in your code, chances are, you'll need to iterate over the values in the array. There's a couple of ways you can do that, some better and more efficient than others depending on what you want to accomplish.

For this post, I'll focus on two ways: some() and every().

some()

The some() method tests whether at least one element in the array passes the test implemented by the provided function. It checks the elements one by one, and if it finds an array element where the function returns a truthy value, some() returns true and does not check the remaining values. Otherwise it returns false.

Lets say you want to check if a contact exists in your contact list:

const contacts = ['Stewie', 'Meg', 'Quagmire', 'Cleveland'];

function checkContacts(arr, val) {
  return arr.some(arrVal => val === arrVal);
}

checkContacts(contacts, 'Lois');   // false
checkContacts(contacts, 'Meg'); // true
Enter fullscreen mode Exit fullscreen mode

When checking for Lois, some() checks the array elements beginning at Stewie to the end, and having not found a match, returns false. When checking for Meg, it stops at Meg and returns true, ignoring the rest of the elements.

every()

This method tests whether all elements in the array pass the test implemented by the provided function. It checks the elements one by one, and if it finds an array element where the function returns a falsy value, every() returns false and does not check the remaining values. Otherwise it returns true.

Lets check if all the names in our contacts list have more than 3 characters:

['Stewie', 'Meg', 'Quagmire', 'Cleveland'].every(contact => contact.length >= 4); // false
['Stewie', 'Megan', 'Quagmire', 'Cleveland'].every(contact => contact.length >= 4); // true
Enter fullscreen mode Exit fullscreen mode

The first test returns false since Meg has only 3 characters. Remember: for every(), all the elements in the array have to be truthy for it to return true. Changing Meg to Megan in the second test therefore returns true.

These two methods can come in handy if you need to perform somewhat similar tasks as described above. But of course, they're not the only way. Hope someone finds this useful! :)

Top comments (8)

Collapse
 
joelnet profile image
JavaScript Joel

Great article!

With curry and partial application, you can also use some like this:

const contacts = ['Stewie', 'Meg', 'Quagmire', 'Cleveland'];

const equals = a => b => a === b

contacts.some(equals('Lois')) // false
contacts.some(equals('Meg')) // true

You could also write every like this:

const propGte = prop => val => obj => obj[prop] >= val;
const lengthGte = propGte('length');

['Stewie', 'Meg', 'Quagmire', 'Cleveland'].every(lengthGte(4)); // false
['Stewie', 'Megan', 'Quagmire', 'Cleveland'].every(lengthGte(4)); // true
Collapse
 
nickytonline profile image
Nick Taylor

Thanks for sharing Kinyanjui. You should check out Sarah Edo's Array Explorer. 🔥 It's a great tool for seeing what array methods do.

Collapse
 
wangonya profile image
Kelvin Wangonya

Nice! Let me check it out

Collapse
 
johnsmith0951 profile image
M.Kai

I didn't know every method.
I found it useful. Thank you Kinyanjui.

Collapse
 
wangonya profile image
Kelvin Wangonya

I'm glad you found the post useful Kai

Collapse
 
aarondrs profile image
Aaron Rodríguez

I found .some() this week by accident. Well, it results very useful in many cases. Happy to read your article.

Thanks for sharing

Collapse
 
wangonya profile image
Kelvin Wangonya

Thanks for reading Aaron

Collapse
 
bellatrix profile image
Sakshi

thank you, your explanation was to the point and easy to understand!