Basics of Javascript · Array · every() (method)

Jakub Korch
Nerd For Tech
Published in
4 min readDec 19, 2023

This article is a transcript of my free youtube series about basics of web development. If you prefer watching over reading, feel free to visit my channel “Dev Newbs”.

Hey everyone! Welcome back to DewNewbs channel and the JavaScript Basics series — this time about the Array object. I’m Jacob, and today we’re going to explore a powerful array method that can make your life a whole lot easier. Introducing Array method every().

The method every() tests whether all elements in the array pass the test which is implemented by the provided function. It will return a Boolean value of true or false depending whether the test was successful or not. Let’s check the simple case in the first example.

// Example 1
// basic usage

console.log("EXAMPLE 1");
console.log("--------------------------------------------------");

const isEven = (x) => x % 2 == 0

// create an array with only even numbers
const numbers = [14, 10, 2874, 369852, 0]

// check if all numbers are even
console.log(numbers.every(isEven))
// returns true

// add odd number
numbers.push(13)

// check again if all numbers are even
console.log(numbers.every(isEven))
// returns false

We are creating a function that checks whether a number is odd or even. Then we create an array with only even numbers and we test whether the method every() confirms that by returning true. It does.

Afterwards, to check if it is also capable of returning false, we add an odd number to our array and we test again. This time method returns false.

We showed the example where the callback function used for testing only required one argument — the element itself. But the callback function can actually receive three arguments. First one being the current element itself, second one is the index of the current element and last one is an array the method has been called upon.

Let’s see how the method deals with more complex use cases leveraging the second and third argument of the callback function.

// Example 2
// advanced use

console.log("EXAMPLE 2");
console.log("--------------------------------------------------");

const valueGreaterThanIndex = (x, i) => x > i

console.log(numbers.every(valueGreaterThanIndex))
// returns false

const numbers2 = [10, 14, 2874, 369852]

console.log(numbers2.every(valueGreaterThanIndex ))
// returns true

console.log("--------------------------------------------------");

const isIncreasing = (num, idx, arr) => {
if (idx === 0) return true;
return num > arr[idx - 1];
}

console.log(numbers2 .every(isIncreasing));
// returns true

numbers2.push(134)

console.log(numbers2 .every(isIncreasing));
// returns false

First use case handles the situation, where we compare whether the value of an element is greater than its index. We defined items in a way so the test would pass and we indeed get true as the result of the every() method.

The second use case works with all three arguments. We need both index and the reference to the whole array, in order to compare whether the current element is always greater than the previous one. Of course with the exception of the first one. We test whether items in an array are always increasing and for our array numbers2 it is true. But once we add a smaller number than the last one at the end of the array, this is no longer true and results of the every() method reflect that as well.

The next examples show how every() method handles sparse arrays. You might be surprised.

// Example 3
// sparse arrays

console.log("EXAMPLE 3");
console.log("--------------------------------------------------");

console.log(["a",,"c"].every((x) => x !== undefined));
// returns true

console.log(["a",,"a"].every((x) => x === "a"));
// returns true

As I mentioned, it might surprise you, but the every() method actually ignores and skips over the items in an array that are empty. And it kind of makes sense. Since we know they are empty, there is really no reason to test them on anything besides not being there.

So in the first example, we are actually testing whether all non-empty elements fulfill a given condition and the same thing in the second example.

Last exercise checks the behavior of the array-like object in conjunction with the every() method. Usual stuff. Let’s see it in action.

// Example 4
// array-like object

console.log("EXAMPLE 4");
console.log("--------------------------------------------------");

const arrayLike = {
length: 3,
0: "a",
2: "c",
3: 145.2
};

console.log(Array.prototype.every.call(arrayLike, (x) => typeof x === "string"));
// returns true

arrayLike [1] = 3.14;

console.log(Array.prototype.every.call(arrayLike, (x) => typeof x === "string"));
// returns false

As with all the other methods, the every() method heavily relies on the property length. So if there is any numerical property outside of the range specified by the length property, the method does not even consider it, so it does not matter what value it contains. In our case, we are testing whether all the elements in an array-like object are of type string. But the property with index 3 is of type number. But it does not really matter, because the every() method ignores this property.

So when the test is executed, the result is true. On the other hand, once we modify the empty element at index 1 with a numeric value, the test no longer passes and the result is false.

That was everything I have prepared for you today. I think every() method is quite cool and useful, but oftentimes overlooked. I will for sure use it more frequently from now on. As usual, thanks for your attention and I will see you in the next video.

--

--

Nerd For Tech
Nerd For Tech

Published in Nerd For Tech

NFT is an Educational Media House. Our mission is to bring the invaluable knowledge and experiences of experts from all over the world to the novice. To know more about us, visit https://www.nerdfortech.org/.

Jakub Korch
Jakub Korch

Written by Jakub Korch

Web enthusiast, programmer, husband and a father. Wannabe entrepreneur. You name it.

No responses yet