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>
function ispositive(element, index, array)
{
return element > 0;
}
var arr = [ 11, 89, 23, 7, 98 ];
var value = arr.every(ispositive);
console.log( value );
</script>
|
Output:
true
Example 2:
JavaScript
<script>
function isodd(element, index, array)
{
return (element % 2 == 1);
}
var arr = [ 11, 89, 23, 7, 98 ];
var value = arr.every(isodd);
console.log( value );
</script>
|
Output:
false
Last Updated :
18 Jun, 2020
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...