Check if all Values in Array are Equal in JavaScript

avatar

Borislav Hadzhiev

Sat Oct 16 20213 min read

banner

Photo by Brooke Cagle

Check if all Values in an Array are Equal #

To check if all values in an array are equal:

  1. Call the every() method, passing it a function.
  2. The function should 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.

In the example, we check if the current element is equal to the element at position 0. If the condition is met for all elements, we can conclude that 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
How you need to handle this entirely depends on your application. If you need to consider an empty array as an array where all values are equal, you're fine.

However if you need to 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.

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

An alternative approach is to use a Set object to achieve the same result.

To check if all values in an array are equal, pass the array to the Set constructor and access the size property of the Set. The Set object only stores unique values, so if it has a length of 1, all array elements have an equal value 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, we know that 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 to your code.

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 elements, if it does we pass the array to the Set constructor.

In all other cases, we return false.

Join my newsletter

I'll send you 1 email a week with links to all of the articles I've written that week

Buy Me A Coffee