‘every’
method return true, if all elements in the array satisfy given predicate, else
false.
Syntax
arr.every(callback[,
thisArg])
callback is a function that takes following
three arguments.
Argument
|
Description
|
currentValue
|
The
current element being processed in the array.
|
index
|
The
index of the current element being processed in the array.
|
array
|
The
array that every() is beingcalled upon.
|
thisArg
Optional.
Value to use as this when executing callback.
function even(num){
return
((num%2) == 0);
}
arr1.every(even); // Return true if
every element of array is even, else false.
every.html
<!DOCTYPE html> <html> <head> <title>Array every</title> </head> <body> <script type="text/javascript"> var arr1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; var arr2 = [2, 4, 6, 8]; function even(num) { return ((num % 2) == 0); } document.write("Is all numbers even in arr1 : " + arr1.every(even) + "<br />"); document.write("Is all numbers even in arr2 : " + arr2.every(even)); </script> </body> </html>
Open above
page in browser, you will get following text.
Is all
numbers even in arr1 : false
Is all
numbers even in arr2 : true
No comments:
Post a Comment