Use .every() to Validate JavaScript Array Values
The best method for checking all elements in an array
Arrays are a mainstay data structure; learning different and optimal ways to work with them is a critical skill for any programmer. Arrays have built-in methods intended to replace for loops.
The map(), filter(), and reduce() methods get most of the attention, but they’re not the only ones. If your task at hand is to validate all elements in an array, then you need to be using the every() method.
The .every() Method
According to the MDN web docs, the every() method is called by passing a callback function. Each element in the arrow will be passed to the callback and if all elements return true, then every() will return true.function isFive(n) {
return n == 5;
}var nums = [5,5,5];console.log(nums.every(isFive)); // true
Our code example can be optimized in a few ways. If the callback will only be used once, an anonymous arrow function can be used in its place.var nums = [5,5,5];// full arrow syntax
console.log(nums.every((n) => { return n == 5; }));// shorthand arrow syntax
console.log(nums.every(n => n == 5));
If you’re working in an environment that supports ES6, you should be using let or const instead of var to define the array.// use if nums will change
let nums = [5,5,5];// use if nums will not change
const nums = [5,5,5];
What About a for Loop?
If you’re clinging on to using Vanilla for loops, then let’s take a look at the previous example refactored as a for loop.let isFive = true;
let nums = [5,5,5];for(let i=0; i < nums.length; i++) {
if(nums[i] != 5) { isFive = false; }
}
To me, the for loop is longer and harder to read. Using every() — regardless of what the actual callback is — lets the reader know that the goal is to validate all elements.
Using a for…of loop yields similar results.let isFive = true;
let nums = [5,5,5];for(n of nums) {
if(n != 5) { isFive = false; }
}
Also, note that both of these examples require a negation, which further convolutes your code.
Concerns With the every() Method
Unfortunately, every() is not perfect for all scenarios; there are some challenges with the method.
The method is not universally compatible. As we can see from this table, courtesy of W3Schools, the method is widely accepted but there are some exceptions.

The method also has fringe behaviors such as returning true when an empty array is passed.let nums = [];console.log(nums.every(n => n == 5)); // true
This is because the callback is never invoked as there are no elements to pass to it. If you’d expect the empty array to return false, you can nest the callback within an if statement.
Since we’ve been doing some refactoring, let’s take another crack at optimizing the if statement with the ternary operator.let nums = [];
let result = (nums.length) ? nums.every(n => n==5) : false;
Conclusion
Use the every() method to efficiently and semantically validate your array elements.
If this is new to you, what are some blocks you plan on refactoring? If you’re a seasoned user, what are your favorite use cases? Share your experiences below and thanks for reading!