JavaScript Array some() Method with Example
In this tutorial, we will learn about JavaScript Array some() method with the help of example programs.
The some() method is a very analogous to every() method. It also iterates over each element in an array until a certain test condition defined in a callback function returns true.
If it finds an element that passes the test condition, it will stop there and the method returns true. The some() method will not run further if an element passes the test.
Otherwise, it will return false if it reaches the end of an array without any of the elements returning true.
Array.some() method is useful in JavaScript if you want to check an element in the array passes a test. Let’s understand it with the help of an example.
Program code 1:
<script> // Defining an array. let nums = [1, 3, 5, 9, 10, 11]; // Create a test function that will return an even number. function isEven(element) { console.log("Checking " +element); return element % 2 === 0; } // Checks whether an array nums contains at least one even number. console.log("Is any even number: " +nums.some(isEven)); </script>
Output: Checking 1 Checking 3 Checking 5 Checking 9 Checking 10 true
Now you can observe in the output the some() method stops at 10 once the test condition is passed. Let’s write the same program using arrow function.
Program code 2:
<script> // Defining an array. let nums = [1, 3, 5, 9, 10, 11]; // Create a test function that will return an even number. const isEven = (element) => element % 2 === 0; console.log(nums.some(isEven)); </script>
Output: true
[adinserter block=”5″]
JavaSctipt Array some() Syntax
The general syntax of some() method in JavaScript array is:
// Arrow function arr.some((element) => { /* ... */ } ) arr.some((element, index) => { /* ... */ } ) arr.some((element, index, array) => { /* ... */ } ) // Callback function arr.some(callbackFn) arr.some(callbackFn, thisArg) // Inline callback function arr.some(function(element) { /* ... */ }) arr.some(function(element, index) { /* ... */ }) arr.some(function(element, index, array){ /* ... */ }) arr.some(function(element, index, array) { /* ... */ }, thisArg)
Here, arr is the name of an array.
Array some() Parameters
The Array.some() method can take three parameters that are:
- callbackFn – The callback function to execute on each array element; returns true if an element passes the callback function. Otherwise, returns false. The function can take the following arguments:
- element – The current element to be passed in the array.
- index – The index of the current element being passed in the array.
- arr – The name of array on which some() method is invoked.
- thisArg – The value to use as this when executing the callback function. By default, it is undefined and optional.
Array some() Return value
The some() method returns a truthy value for at least one element in the array if it passes the test condition defined in a callback function. If no elements pass the test condition, the some() method returns false.
[adinserter block=”2″]
Note: If you call some() method on an empty array, the method returns false for any condition.
Array some() Method Example Programs
Let’s take some example programs based on the JavaScript array some() method.
Testing value of Array elements
The following code checks whether any element in the array is bigger than 10.
Program code 3:
<script> let array1 = [2, 5, 6, 8, 10, 12]; function isBiggerThan10(element) { return element > 10; } console.log(array1.some(isBiggerThan10)); let array2 = [2, 4, 6, 8, 3]; console.log(array2.some(isBiggerThan10)); </script>
Output: true false
In this example, we have used the some() method to find out whether any element of the array1 array contains a value greater than 10.
At first, we have created an array named array1. Then, we have created a callback function named isBiggerThan10 that will return a value greater than 10.
After it we have passed callback function to the some() method as array1.some(isBiggerThan10) that tests for elements greater than 10 and returns true. Similarly, for array2.
Testing Array elements using Arrow functions
We can also use arrow functions in JavaScript that provide a shorter syntax for the same test.
Program code 4:
['C', 'C++', 'JavaScript', 'Python', 'Ruby', 'Java'].some(word => word.length > 7); // true [2, 1, 8, 5, 3, 9].some(x => x > 10); // false [13, 5, 8, 1, 3].some(x => x > 10); // true
Checking whether a value exists in an array
Let’s create a JavaScript program to check whether a value exists in an array.
Program code 5:
<script> let fruits = ['banana', 'apple', 'mango', 'orange']; function checkAvailability(arr, val) { let x = function(arrVal) { return val === arrVal; }; return arr.some(x); } console.log(checkAvailability(fruits, 'banana')); console.log(checkAvailability(fruits, 'kela')); </script>
Output: true false
Checking whether a value exists in an array using Arrow function
Let’s create a JavaScript program to check whether a value exists in an array using arrow function.
Program code 6:
<script> let fruits = ['banana', 'apple', 'mango', 'orange']; function checkAvailability(arr, val) { return arr.some(arrVal => val === arrVal); } console.log(checkAvailability(fruits, 'banana')); console.log(checkAvailability(fruits, 'kela')); </script>
Output: true false
Difference between some() and every() method in JavaScript
The fundamental difference between some() and every() method is:
The every() method returns true if each element in the array passes the test condition defined in the callback function. Otherwise, it returns false.
The some() method returns true if any element in the array passes the test condition defined in the callback function. It returns false if none of the elements in the array passes the test condition.
In this tutorial, you have learned JavaScript Array some() method with many example programs. Hope that you will have understood the basic concept of Array.some() method and practiced all programs.
In the next tutorial, we will discuss every() method of JavaScript array with the help of examples.
Thanks for reading!!!