Member-only story

How to Use some() & every() in JavaScript

A tutorial on how to use the array helper methods some() & every() in JavaScript

Codecupdev
JavaScript in Plain English
4 min readMay 12, 2022

--

The some() and every() array methods are two of the array helper methods which were introduced in ES6. Both of the methods return boolean values (true or false). We will start by looking at every.

every()

When we use every() we pass in a callback method (usually an anonymous function). The callback method needs to return a boolean, We can think of this as a test method. The method will get run once per every element within the array which every() is called on. If all the elements pass the test callback method, true will be returned. If one or more elements do not pass the test callback method, false will be returned. Let’s look at a basic example:

const names = ["Abby", "Anna", "Alice"];names.every(function(name) {
return name.includes("A");
});
//Returns ---> true

In the above example, we declare a variable called names using const. We assign to this an array containing three strings. We then use every() on the names array, pass in the callback, and set a name parameter. The name parameter will represent each element from the names array. We return whether the name element…

--

--

Published in JavaScript in Plain English

New JavaScript and Web Development content every day. Follow to join our 3.5M+ monthly readers.

No responses yet

What are your thoughts?