every() Method
The every() method tests whether all elements in the array passed the condition given to the callback function. It returns a boolean value indicating whether all elements satisfy the condition.
Syntax:
array.every(callback(element[, index[, array]])[, thisArg])
Parameters:
- callback: Function to test for each element.
- element: 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 some() was called upon.
- thisArg (Optional): Object to use as this when executing the callback.
Return Value:
It returns true, if all elements of array passed the given condition. Otherwise, it will return false.
Example: The below example implements the every() method with an array in javaScript.
Javascript
const arr1 = [1, 2, 3, 4, 5]; const arr2 = [3, 5, 8, 9, 11]; const res1 = arr1.every(num => num < 10); const res2 = arr2.every(num => num < 10); console.log(res1, res2); |
true false
How to use every() or some() methods in JavaScript ?
In JavaScript, the every() and some() methods are array methods used to check the elements of an array based on a given condition.
Table of Content
- every() Method
- some() Method