TypeScript Array some() Method
Last Updated :
12 Jul, 2024
Improve
The Array.some() method in TypeScript is used to check if at least one element in the array passes the test implemented by the provided function.
Syntax:
array.some(callback[, thisObject])
Parameter: This method accepts two parameters as mentioned above and described below:
- callback: This parameter is the Function to test for each element.
- thisObject: This is the Object to use as this parameter when executing callback.
Return Value: This method returns true if some element in this array satisfies the provided testing function.
The below example illustrates the Array some() method in TypeScriptJS:
Example 1: Checking for Positive Numbers
In this example, the some() method checks if there is at least one positive number in the array.
let numbers: number[] = [-1, -2, 3, -4, -5];
let hasPositive = numbers.some(num => num > 0);
console.log(hasPositive); // Output: true
Output:
true
Example 2: Checking for String Length
In this example, the some() method checks if there is at least one string with a length greater than 5.
let words: string[] = ["hello", "world", "TypeScript", "JS"];
let hasLongWord = words.some(word => word.length > 5);
console.log(hasLongWord); // Output: true
Output:
true