some() Function is used to check if any one element of the array satisfies a specific condition.
Let us say, we have a Array that contains five numbers, 5, 8, 2, 81 and 9.
And we want to check if any one of the values in the arrays is less than 10.
Well! There are various ways to do it. But some() gives us an elegant way to do it.
Let us see in the below example.
<html> <body> <script> function numberCheck(i) { return i < 10 } var x = [5, 8, 2, 81, 9] var y = x.some(numberCheck) if (y == true) { document.write("At least one element in the Array is less tha 10") } else if(y == false) { document.write("No!! Not all the numbers in the Array are less tha 10") } </script> </body> </html>
So, we have an Array that has 5 numbers.
And we want to check, if any one of the number is less than 10. If any of the number is less than 10 then the execution should stop returning true.
We have defined a function called numberCheck(), that checks if a number is less than 10.
function numberCheck(i) { return i < 10 }
And we have called the above function from some() function.
var y = x.some(numberCheck)
And some() function iterates 5 times(i.e. The length of the array), calling numberCheck() function.
And if at any time it finds a value that is less than 10, the some() function stops returning true.
And thus we got the below output.
At least one element in the Array is less tha 10
For the if statement.
if (y == true) { document.write("At least one element in the Array is less tha 10") } else if(y == false) { document.write("No!! Not all the numbers in the Array are less tha 10") }