some() in JavaScript

some() in JavaScript

·

1 min read

The some() method in JS is somewhat similar to every() method in terms of arguments that it has and the type of value it returns (boolean), but its functionality is a bit different. The some() method checks whether any single element of an array meets the condition which is written inside its callback function. some() return a true if any element of the array satisfies the condition written in its callback. Sounds difficult? Let's have some examples.

let array1 = [2, 45, 12, 54, 67, 77];
var isDivisibleByTwo=
array1.some(element=>{
    return element%2 ===0;
});
console.log(isDivisibleByTwo);
//output : true

The above code returns true as the first element itself satisfies the function. Let's have one more simple example:

let arr1 = [10,11,12,13,14,15]
let arr2 = [18]
let hasSomeElement = arr2.some((element)=>{
    return arr1.includes(element)
})
console.log(hasSomeElement);
//false

Since no element of arr2 is inside arr1, hence the value returned is false.