-
Notifications
You must be signed in to change notification settings - Fork 2
JS Array Prototype Every
The every()
method tests whether all elements in the array pass the test implemented by the provided function.
Syntax
arr.every(callback[, thisArg])
-
callback Function to test for each element, taking three arguments:
-
currentValue (required)
The current element being processed in the array.
-
index (optional)
The index of the current element being processed in the array.
-
array (optional)
The array every was called upon.
-
-
thisArg Optional. Value to use as this when executing callback.
The every
method calls the callback
function one time for each array element, in ascending index order, until the callback
function returns false. If an element that causes callback
to return false is found, the every method immediately returns false
. Otherwise, the every method returns true
.
The callback function is not called for missing elements of the array.
In addition to array objects, the every method can be used by any object that has a length property and that has numerically indexed property names. every
does not mutate the array on which it is called.
function isBigEnough(element, index, array) {
return element >= 10;
}
[12, 5, 8, 130, 44].every(isBigEnough); // false
[12, 54, 18, 130, 44].every(isBigEnough); // true
// Define the callback function.
function CheckIfEven(value, index, ar) {
document.write(value + " ");
if (value % 2 == 0)
return true;
else
return false;
}
// Create an array.
var numbers = [2, 4, 5, 6, 8];
// Check whether the callback function returns true for all of the
// array values.
if (numbers.every(CheckIfEven))
document.write("All are even.");
else
document.write("Some are not even.");
// Output:
// 2 4 5 Some are not even.
Learn to code and help nonprofits. Join our open source community in 15 seconds at http://freecodecamp.com
Follow our Medium blog
Follow Quincy on Quora
Follow us on Twitter
Like us on Facebook
And be sure to click the "Star" button in the upper right of this page.
New to Free Code Camp?
JS Concepts
JS Language Reference
- arguments
- Array.prototype.filter
- Array.prototype.indexOf
- Array.prototype.map
- Array.prototype.pop
- Array.prototype.push
- Array.prototype.shift
- Array.prototype.slice
- Array.prototype.some
- Array.prototype.toString
- Boolean
- for loop
- for..in loop
- for..of loop
- String.prototype.split
- String.prototype.toLowerCase
- String.prototype.toUpperCase
- undefined
Other Links