This article is originally published at WebDevIdea.
JavaScript has a lot of useful methods that we can use to easily work with arrays. These JavaScript methods are called higher-order functions. So keep in mind that any function that takes another function as its argument is called a higher-order function.
In this article, we will learn about the two higher-order functions some()
and every()
by covering the differences between them. Let’s get started.
The some method
The higher-order function some()
in JavaScript is used with arrays. It checks if any element in the array passes a test that we provide. If an element in the array passes the test, it returns true
. If none of the elements passes the test, it returns false
.
The method some()
takes a callback function as its argument. The callback function itself takes three arguments:
- The array element(required).
- The element index(optional).
- The array itself(optional).
Let’s have a look at a practical example:
let numbers = [6, 7, 8, 9, 10];
//Using ES5 syntax.
numbers.some(function(number){
return number > 8;
});
// returns true.
//Using ES6 syntax.
numbers.some(number => number > 8);
// returns true.
As you can see above, the method some()
here checks if any number in the array is greater than 8. That's true because we have 9 which is greater than 8. That's why the method some()
returns true.
So the higher-order function some()
iterates through each element in the array until it finds the one that passes the test(greater than 8), then it returns true. If none of the array elements is greater than 8, it will return false.
In addition to that, the method some()
does not change the original array numbers
.
console.log(numbers);
// output: [6, 7, 8, 9, 10]
Here is another example that returns false
:
let numbers = [6, 7, 8, 9, 10];
numbers.some(function(number){
return number < 6;
});
// returns false.
numbers.some(number => number < 6);
// returns false.
The every method
The method every()
is also used with arrays in JavaScript. It checks if all elements in the array pass a test that we provide. If all elements in the array pass the test, it returns true
. If at least one element didn't pass the test, it returns false
.
The method every()
also takes a callback function as its argument. The callback function itself takes three arguments:
- The array element(required).
- The element index(optional).
- The array itself(optional). Have a look at the code example below:
let numbers = [6, 7, 8, 9, 10];
//Using ES5 syntax.
numbers.every(function(number){
return number >= 6;
});
// returns true.
//Using ES6 syntax.
numbers.every(number => number >= 6);
// returns true.
In the example above, the method every()
checks if every number in the array of numbers is greater than or equal to 6. Because all the numbers in the array numbers
are greater than or equal to 6, the function returns true
.
So the higher-order function some()
iterates through each element in the array. If at least one element is not greater than or equal to 6, it will return false
. But if all the array elements pass the test, it will return true
.
In addition to that, the method every()
also does not change the original array numbers
.
console.log(numbers);
// output: [6, 7, 8, 9, 10]
Here is another example:
let names = ['John', 'John', 'Mehdi', 'John'];
//ES5 syntax.
names.every(function(name){
return name === 'John';
});
// returns false.
//ES6 syntax.
names.every(name => name === 'John');
// returns false.
As you can see above, we used the method every()
to check if all the array elements have the name of John
.
Since we have another name Mehdi
in the names array, the function returns false
.
Conclusion
some()
and every()
are two useful array methods that you should know in JavaScript. The first one checks if any element in the array passes a test function. On the other hand, the second one(every) checks if all the elements in the array pass the test function. So that's the only difference between these two higher-order functions in JavaScript.
Top comments (10)
Great post! I feel like you kind of touched on it, but I wanted to make sure it was pointed out:
With
some()
the method will return true IMMEDIATELY upon the first item that passes the test. While withevery()
, the method will return false IMMEDIATELY upon the first item that fails the test.Which means if the first item in an array of 5 passes the test we provide in
some()
then it would return true after only one iteration. Likewise if the first item in an array of 5 would not pass the test we provide toevery()
then it would return false after one iteration.Thank you! That's a good point that I talked about.
Nice article.
Thank you for the tip, I didn't know about it.
I actually want to escape the code but the blog does not allow me to do so. I even used backslash.
Cool these are two javascript methods that don't get enough exposure.
Thank you!
Thanks for sharing! This definitely helps me to simplify my code.
hope you stay safe and healthy!
Thanks for reading! I appreciate it.