Check if all Values in Array are Equal in JavaScript

avatar

Borislav Hadzhiev

5 min

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 will return true if all array elements are equal.
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 if (allAreEqual(arr1)) { // ๐Ÿ‘‡๏ธ this runs console.log('All elements in the array are equal'); } else { console.log('Not all elements in the array are equal'); }

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 Array.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

If you consider an empty array one 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 a Set object #

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.

You can also use a for...of loop to check if all elements in an array are equal.

Check if all Values in an Array are Equal using a for...of loop #

To check if all values in an array are equal:

  1. Use a for...of loop to iterate over the array.
  2. Check if each element is not equal to the first array element.
  3. If the condition is met, set a boolean variable to false and exit the loop.
index.js
function allAreEqual(array) { let areEqual = true; for (const element of array) { if (element !== array[0]) { areEqual = false; break; } } return areEqual; } const arr = [0, 0, 0]; const arr1 = [1, 1, 2]; const arr2 = [1, 1, 1]; console.log(allAreEqual(arr)); // ๐Ÿ‘‰๏ธ true console.log(allAreEqual(arr1)); // ๐Ÿ‘‰๏ธ false console.log(allAreEqual(arr2)); // ๐Ÿ‘‰๏ธ true

The for...of statement is used to loop over iterable objects like arrays, strings, Map, Set and NodeList objects and generators.

On each iteration, we check if the current element is not equal to the first array element.

If the condition is met, we set the areEqual variable to false and break out of the loop.

The for...of loop is quite efficient because it doesn't iterate needlessly once it finds a value that isn't equal to the other array elements.

You can also use a basic for loop to check if all elements in an array are equal.

Check if all Values in an Array are Equal using a for loop #

To check if all values in an array are equal:

  1. Use a for loop to iterate over the array.
  2. On each iteration, check if the current element is not equal to the first array element.
  3. If the condition is never met, all array elements are equal.
index.js
function allAreEqual(array) { let areEqual = true; for (let index = 0; index < array.length; index++) { if (array[index] !== array[0]) { areEqual = false; break; } } return areEqual; } const arr = [0, 0, 0]; const arr1 = [1, 1, 2]; const arr2 = [1, 1, 1]; console.log(allAreEqual(arr)); // ๐Ÿ‘‰๏ธ true console.log(allAreEqual(arr1)); // ๐Ÿ‘‰๏ธ false console.log(allAreEqual(arr2)); // ๐Ÿ‘‰๏ธ true

We used a basic for loop to iterate over the array.

On each iteration, we check if the element at the current index is not equal to the first array element.

If the condition is met, we set the areEqual variable to false and use the break statement to break out of the loop.

You can also use the Array.filter() method to check if all values in an array are equal.

Check if all Values in an Array are Equal using Array.filter() #

To check if all values in an array are equal:

  1. Use the Array.filter() method to iterate over the array.
  2. Check if each array element is equal to the first array element.
  3. Check if the new array's length is equal to the original array's length.
index.js
function allAreEqual(array) { return ( array.filter(element => { return element === array[0]; }).length === array.length ); } const arr = [0, 0, 0]; const arr1 = [1, 1, 2]; const arr2 = [1, 1, 1]; console.log(allAreEqual(arr)); // ๐Ÿ‘‰๏ธ true console.log(allAreEqual(arr1)); // ๐Ÿ‘‰๏ธ false console.log(allAreEqual(arr2)); // ๐Ÿ‘‰๏ธ true

The function we passed to the Array.filter() method gets called with each element in the array.

On each iteration, we check if the current element is equal to the first array element.

The filter() method returns a new array containing the values for which the callback function returned true.

If the length of the new array is equal to the length of the original array, then all array elements are equal.

Note that this approach is not very efficient because you'd have to keep iterating over the array even if a non-matching value is found.

Which approach you pick is a matter of personal preference. I'd use the Array.every() method or the Set() constructor because they're quite performant and easy to read.

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.

Copyright ยฉ 2023 Borislav Hadzhiev