TypeScript | Array every() Method
The Array.every() is an inbuilt TypeScript function which is used to check for all the elements in an array passes the test implemented by the provided function.
Syntax:
array.every(callback[, thisObject])
Parameter: This method accepts two parameter as mentioned above and described below:
- callback : This parameter is the Function to test for each element.
- thisObject : This parameter is the Object to use as this when executing callback.
Return Value: This method returns true if every element in this array satisfies the provided testing function.
Below examples illustrate Array every() method in TypeScript
Example 1:
JavaScript
<script> // Check for positive number function ispositive(element, index, array) { return element > 0; } // Driver code var arr = [ 11, 89, 23, 7, 98 ]; // Check for positive number var value = arr.every(ispositive); console.log( value ); </script> |
Output:
true
Example 2:
JavaScript
<script> // Check for odd number function isodd(element, index, array) { return (element % 2 == 1); } // Driver code var arr = [ 11, 89, 23, 7, 98 ]; // Check for positive number var value = arr.every(isodd); console.log( value ); </script> |
Output:
false
Please Login to comment...