The Array.some()
method is a built-in JavaScript function that tests whether at least one element in an array passes a provided test implemented by a callback function. It stops execution and returns true
when it finds an element that satisfies the condition, making it efficient for use cases where partial matches are sufficient.
Syntax
arr.some(callback(element, index, array), thisArg);
Parameters
Parameter | Description |
---|---|
callback | (Required) A function to test each element of the array. |
element | (Required in callback ) The current element being processed. |
index | (Optional in callback ) The index of the current element being processed. |
array | (Optional in callback ) The array some() was called on. |
thisArg | (Optional) A value to use as this when executing the callback function. Defaults to undefined . |
Return Value
The some()
method returns:
true
if at least one element in the array satisfies the test condition.false
if no elements satisfy the test condition.
Example 1: Checking for a Positive Number
let arr = [-10, -20, 5, -30]; let hasPositive = arr.some(num => num > 0); console.log(hasPositive); // Output: true
Example 2: Testing for Even Numbers
let arr = [1, 3, 5, 7, 8]; let hasEven = arr.some(num => num % 2 === 0); console.log(hasEven); // Output: true
Example 3: Using some()
with Strings
let arr = ['HTML', 'CSS', 'JavaScript', 'jQuery']; let hasExist = arr.some(item => item === 'JavaScript'); console.log(hasExist); // Output: true
Example 4: Using some()
with Objects
let users = [ { name: 'John', age: 25 }, { name: 'Alice', age: 30 }, { name: 'Bob', age: 20 } ]; let hasAdult = users.some(user => user.age >= 18); console.log(hasAdult); // Output: true
Example 5: Using thisArg
Parameter
let arr = [10, 20, 30]; let context = { threshold: 25 }; let result = arr.some(function(item) { return item > this.threshold; }, context); console.log(result); // Output: true
Example 6: Checking for Undefined Values
let data = [1, undefined, 3]; let hasUndefined = data.some(value => value === undefined); console.log(hasUndefined); // Output: true
Supported Browsers
Browser | Support |
---|---|
Chrome | 1+ |
Firefox | 1.5+ |
Safari | 3+ |
Edge | 12+ |
Opera | 10.5+ |
Internet Explorer | 9+ |