Difference Between some() and every() in JavaScript
Tags: javascript, webdev, codenewbie, programming
Published: 2021-03-28 03:06:47

This article is originally published at [WebDevIdea](https://webdevidea.com/blog/some-vs-every-in-javascript/).

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.