JavaScript · January 30, 2021

.some() and .every() method in JavaScript

If you know the basics of JavaScript, I can assume that you are familiar with JavaScript’s array. The array is a Data Structure to store more than one value. JavaScript has so many useful built-in array methods. Some methods we use frequently. Such as forEach, map, filter, etc. You may be familiar already.

In this article, we are going to know about some and every method. Both are built-in array methods.

Both methods returns boolean. These methods check each of the elements from an array based on our condition. But there is two key difference. Let’s see the difference.

.some()

// Example 1
const array1 = [1, 5, 6, 9, 8]
let res1 = array1.some((element) => typeof element === 'number')
console.log(res1) // true
// Example 2
const array2 = ['hello', 'world', 1, false]
let res2 = array2.some((element) => typeof element === 'number')
console.log(res2) // true
// Example 3
const array3 = ['hello', 'world', true]
let res3 = array3.some((element) => typeof element === 'number')
console.log(res3) // false

In this method, if our callback function returns true for at least one element in the array, then some method returns true. Otherwise, it will be false. See example 3.

.every()

// Example 1
const array1 = [1, 5, 6, 9, 8]
let res1 = array1.every((element) => typeof element === 'number')
console.log(res1) // true
// Example 2
const array2 = ['hello', 'world', 1, false]
let res2 = array2.every((element) => typeof element === 'number')
console.log(res2) // false
// Example 3
const array3 = ['hello', 'world', true]
let res3 = array3.every((element) => typeof element === 'number')
console.log(res3) // false

This method is the opposite of the previous one. If our callback function returns true for every element in the array, then every method returns true. If not, then it will be false. See the examples.