Check if all Values in Array are Equal in JavaScript

avatar

Borislav Hadzhiev

Last updated: Jul 25, 2022

banner

Photo from Unsplash

Check if all Values in an Array are Equal #

To check if all values in an array are equal:

  1. Use the Array.every() method to iterate over the array.
  2. Check if each array element is equal to the first one.
  3. The every method only returns true if the condition is met for all array elements.
index.js
const arr1 = [1, 1, 1]; const arr2 = [1, 1, 2]; function allAreEqual(array) { const result = array.every(element => { if (element === array[0]) { return true; } }); return result; } console.log(allAreEqual(arr1)); // 👉️ true console.log(allAreEqual(arr2)); // 👉️ false

The function we passed to the Array.every method gets called with each element in the array until it returns a falsy value or iterates over the entire array.

If the function returns a falsy value, the every method short-circuits and returns false.

On each iteration, we check if the current element is equal to the element at position 0. If the condition is met for all elements, then they are all equal.

When the every method is called on an empty array it returns true for any condition.
index.js
const arr1 = []; function allAreEqual(array) { const result = array.every(element => { if (element === array[0]) { return true; } }); return result; } console.log(allAreEqual(arr1)); // 👉️ true
Whether you need to handle this depends on your use case. If consider an empty array as an array where all values are equal, you don't need to do anything.

However, if you consider an empty array one where not all elements are equal, use the following solution.

index.js
const arr1 = []; const arr2 = [1, 1, 1]; function allAreEqual(array) { if (array.length > 0) { const result = array.every(element => { if (element === array[0]) { return true; } }); return result; } return false; } console.log(allAreEqual(arr1)); // 👉️ false console.log(allAreEqual(arr2)); // 👉️ true
We first check for the array's length and we only call the every method if the array contains at least 1 element.

This is entirely use case specific. For example, you might want to check if the array has at least 2 elements to return true from the function.

An alternative approach is to use a Set object.

Check if all Values in an Array are Equal using Set #

To check if all values in an array are equal:

  1. Pass the array to the Set constructor and access the size property.
  2. The Set object only stores unique values.
  3. If the Set has a length of 1, then all array elements are equal or the array only contains 1 element.
index.js
const arr1 = [1, 1, 1]; const arr2 = [1, 1, 2]; function allAreEqual(array) { const result = new Set(array).size === 1; return result; } console.log(allAreEqual(arr1)); // 👉️ true console.log(allAreEqual(arr2)); // 👉️ false

The Set object allows us to store unique values and removes all duplicates automatically.

If we pass it an array containing the same value multiple times, it would only get added once to the Set.

index.js
console.log(new Set([1, 1, 1])); // 👉️ { 1 }

The size property of the Set allows us to get the number of values stored in the Set.

If the number of values in the Set is equal to 1, then all of the values in the array are equal or the array only contains one element.

If you only want to return true if the array contains at least 2 elements, add the following if statement.

index.js
const arr1 = [1]; const arr2 = [1, 1, 1]; function allAreEqual(array) { if (array.length > 1) { const result = new Set(array).size === 1; return result; } else { return false; } } console.log(allAreEqual(arr1)); // 👉️ false console.log(allAreEqual(arr2)); // 👉️ true

We first make sure the array contains more than 1 element. If it does, we pass the array to the Set constructor.

In all other cases, we return false.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.