JavaScript arr.some() method checks whether at least one of the elements of the array satisfies the condition checked by the argument method.
Syntax:
arr.some(callback(element,index,array),thisArg);
Parameters:
- callback: This parameter holds the function to be called for each element of the array.
- element: The parameter holds the value of the elements being processed currently.
- index: This parameter is optional, it holds the index of the currentValue element in the array starting from 0.
- array: This parameter is optional, it holds the complete array on which Array.every is called.
- thisArg: This parameter is optional, it holds the context to be passed as this is to be used while executing the callback function. If the context is passed, it will be used like this for each invocation of the callback function, otherwise undefined is used as default.
Return value:
This method returns true even if one of the elements of the array satisfies the condition(and does not check the remaining values) implemented by the argument method. If no element of the array satisfies the condition then it returns false.
Example 1: In this example, we will check whether the values are equal using the some() method.
JavaScript
function checkAvailability(arr, val) {
return arr.some(function (arrVal) {
return val === arrVal;
});
}
function func() {
let arr = [2, 5, 8, 1, 4];
console.log(checkAvailability(arr, 2));
console.log(checkAvailability(arr, 87));
}
func();
|
Example 2: In this example the method some() checks for any number that is greater than 5. Since there exists an element that satisfies this condition, thus the method returns true.
JavaScript
function isGreaterThan5(element, index, array) {
return element > 5;
}
function func() {
let array = [2, 5, 8, 1, 4];
let value = array.some(isGreaterThan5);
console.log(value);
}
func();
|
Example 3: In this example the method some() checks for any number that is greater than 5. Since there exists no elements that satisfy this condition, thus the method returns false.
JavaScript
function isGreaterThan5(element, index, array) {
return element > 5;
}
function func() {
let array = [-2, 5, 3, 1, 4];
let value = array.some(isGreaterThan5);
console.log(value);
}
func();
|
Example 4: In this example the method some() checks for 2 and 87 in the array. Since only 2 is available therefore the method returns true for the first query while it returns false for the second query.
Javascript
let arr = [2, 5, 8, 1, 4]
function checkAvailability(arr, val) {
return arr.some(
function (arrVal) {
return val === arrVal;
});
}
console.log((checkAvailability(arr, 2)));
console.log((checkAvailability(arr, 87)));
|
We have a complete list of Javascript Array methods, to check those please go through this Javascript Array Complete reference article.
Supported Browsers:
- Google Chrome 1 above
- Edge 12 and above
- Firefox 1.5 and above
- Internet Explorer 9 and above
- Opera 9.5 and above
- Safari 3 and above
We have a Cheat Sheet on Javascript where we covered all the important topics of Javascript to check those please go through Javascript Cheat Sheet-A Basic guide to JavaScript.
Learn to code easily with our course
Coding for Everyone. This course is accessible and designed for everyone, even if you're new to coding. Start today and join millions on a journey to improve your skills!Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Commit to GfG's Three-90 Challenge! Purchase a course, complete 90% in 90 days, and save 90% cost click here to explore.
Last Updated :
20 Dec, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...