## JavaScript Array every() Method
The `every()` method tests whether all elements in an array pass the test implemented by the provided callback function. It returns a Boolean value (`true` or `false`).
---
### Quick Example
Check if all values in the `ages` array are greater than or equal to 18:
```javascript
const ages = [32, 33, 16, 40];
function checkAdult(age) {
return age >= 18;
}
console.log(ages.every(checkAdult));
// Output: false (since 16 is less than 18)
```
---
## Definition and Usage
The `every()` method executes a callback function once for each element present in the array until it finds one where the callback returns a falsy value.
* **Short-circuiting behavior:** If the method finds an array element where the callback function returns `false`, `every()` immediately returns `false` and stops iterating through the remaining elements.
* **Success condition:** If the callback function returns `true` for all elements, `every()` returns `true`.
* **Empty Arrays:** `every()` will not run the callback function for empty arrays. It immediately returns `true` for any condition on an empty array (this is known as vacuously true).
* **Immutability:** `every()` does not mutate (change) the original array.
---
## Syntax
```javascript
array.every(callbackFunction(currentValue, index, arr), thisValue)
```
### Parameter Values
| Parameter | Description |
| :--- | :--- |
| `callbackFunction` | **Required.** A function to run for each element in the array. |
| `thisValue` | **Optional.** A value to be used as `this` when executing the callback function. If omitted, `undefined` is used as its `this` value. |
#### Callback Function Arguments
| Argument | Description |
| :--- | :--- |
| `currentValue` | **Required.** The value of the current element being processed in the array. |
| `index` | **Optional.** The index of the current element being processed in the array. |
| `arr` | **Optional.** The array object that `every()` was called upon. |
---
## Technical Details
| Feature | Description |
| :--- | :--- |
| **Return Value** | **Boolean**. Returns `true` if the callback function returns a truthy value for every array element; otherwise, `false`. |
| **JS Version** | ECMAScript 5 (ES5) / JavaScript 1.6 |
| **Browser Support** | Supported in all modern browsers (Chrome, Edge, Firefox, Safari, Opera, and IE9+). |
---
## More Examples
### Example 1: Using Arrow Functions (ES6+)
Modern JavaScript developers frequently use arrow functions with `every()` for cleaner, more concise code.
```javascript
const numbers = [10, 20, 30, 40];
// Check if all numbers are even
const allEven = numbers.every(num => num % 2 === 0);
console.log(allEven); // Output: true
```
### Example 2: Checking User Input Against Array Elements
This example demonstrates how to check if all ages in an array are greater than or equal to a dynamic user-defined threshold.
```html
Minimum Age:
Are all ages valid?
```
### Example 3: Using the `thisValue` Parameter
You can pass an object to be used as `this` inside the callback function.
```javascript
const validator = {
minLimit: 10,
maxLimit: 100,
isWithinRange(value) {
return value >= this.minLimit && value <= this.maxLimit;
}
};
const scores = [15, 45, 85, 99];
// Pass 'validator' as the second argument (thisValue)
const allValid = scores.every(function(score) {
return this.isWithinRange(score);
}, validator);
console.log(allValid); // Output: true
```
---
## Key Considerations & Best Practices
1. **Performance (Short-circuiting):** `every()` is highly efficient for large arrays because it stops execution as soon as it encounters the first element that fails the condition.
2. **Empty Arrays:** Calling `every()` on an empty array `[]` will always return `true` regardless of the condition. Always ensure this behavior aligns with your application logic.
3. **No Mutation:** While `every()` does not mutate the original array, the callback function *can* mutate it. Modifying the array during iteration is highly discouraged as it can lead to unpredictable behavior.