JavaScript Array every()
Syntax & Examples
Array.every() method
The every() method of the Array class in JavaScript tests whether all elements in the array pass the test implemented by the provided function. It returns a Boolean value.
Syntax of Array.every()
There are 2 variations for the syntax of Array.every() method. They are:
every(callbackFn)
Parameters
Parameter | Optional/Required | Description |
---|---|---|
callbackFn | required | A function to test each element in the array. It takes three arguments: element, index, and array. |
This method returns true if every element in the calling array satisfies the testing function.
Returns value of type Boolean
.
every(callbackFn, thisArg)
Parameters
Parameter | Optional/Required | Description |
---|---|---|
callbackFn | required | A function to test each element in the array. It takes three arguments: element, index, and array. |
thisArg | optional | A value to use as this when executing callbackFn. |
This method returns true if every element in the calling array satisfies the testing function, using thisArg as the value of this inside callbackFn.
Returns value of type Boolean
.
✐ Examples
1 Using every() method to check if all elements are greater than a value
In JavaScript, we can use the every() method to check if all elements in an array are greater than a specified value.
For example,
- We define an array variable arr with elements [10, 20, 30, 40, 50].
- We define a callback function isGreaterThanFive to check if each element is greater than 5.
- We use the every() method with isGreaterThanFive to test all elements in the array.
- The result is stored in the variable result.
- We log result to the console using console.log() method to see if all elements pass the test.
JavaScript Program
const arr = [10, 20, 30, 40, 50];
const isGreaterThanFive = element => element > 5;
const result = arr.every(isGreaterThanFive);
console.log(result);
Output
true
2 Using every() method to check if all elements are strings
We can use the every() method to check if all elements in an array are strings.
For example,
- We define an array variable strArr with elements ['apple', 'banana', 'cherry'].
- We define a callback function isString to check if each element is a string.
- We use the every() method with isString to test all elements in the array.
- The result is stored in the variable result.
- We log result to the console using console.log() method to see if all elements pass the test.
JavaScript Program
const strArr = ['apple', 'banana', 'cherry'];
const isString = element => typeof element === 'string';
const result = strArr.every(isString);
console.log(result);
Output
true
3 Using every() method with a thisArg
We can use the every() method with a thisArg to test all elements in an array using a specific context.
For example,
- We define an array variable arr with elements [1, 2, 3, 4, 5].
- We define an object context with a threshold property set to 4.
- We define a callback function isLessThanThreshold to check if each element is less than the threshold property of the context object.
- We use the every() method with isLessThanThreshold and context as thisArg to test all elements in the array.
- The result is stored in the variable result.
- We log result to the console using console.log() method to see if all elements pass the test.
JavaScript Program
const arr = [1, 2, 3, 4, 5];
const context = { threshold: 4 };
function isLessThanThreshold(element) {
return element < this.threshold;
}
const result = arr.every(isLessThanThreshold, context);
console.log(result);
Output
false
Summary
In this JavaScript tutorial, we learned about every() method of Array: the syntax and few working examples with output and detailed explanation for each example.