Check If All Values In Array Are Equal In JavaScript

To check if all values in array are equal in JavaScript, you can use any one of the following methods –

  1. Use the Array.every() method – The every() method tests whether all elements in the array pass the test implemented by the provided function.
  2. Use the Array.reduce() method – The reduce() method applies a function against an accumulator and each element in the array (from left to right) to reduce it to a single value. If you have an array of length 1, the result is that element.
  3. Use the Set object – The Set object lets you store unique values of any type, whether primitive values or object references.

Let’s discuss each of these methods in detail.

check if all array values are equal in JavaScript

Method 1: Array.every() Method To Check If All Values In Array Are Equal In JavaScript

The every() method tests whether all elements in the array pass the test implemented by the provided function. It returns a Boolean value – true if all values passes the test and false otherwise.

Consider the following example –

var arr = [1,1,1];

var allEqual = arr.every(function(element, index, array){

  return element === array[0];

});

console.log(allEqual); // true

In the above code, we have created an array arr of type number. We have passed this array to the every() method along with a callback function that compares each element of the array with the first element and returns true if they are equal and false otherwise. The output of this code would be –

true // all values in arr are equal i.e., 1 === 1 === 1

You can also use the arrow function syntax as follows –

var arr = ['a','a','a'];

var allEqual = arr.every(element => element === arr[0]);

console.log(allEqual); // true

This method will also return true for an empty array –

var arr = [];

var allEqual = arr.every(element => element === arr[0]);

console.log(allEqual); // true

This might be fine for your use case, but if you want to return false for an empty array, you can check for that separately in the code –

var arr = [];

var allEqual = false;

if(arr.length > 0){

  allEqual = arr.every(element => element === arr[0]);

}

console.log(allEqual); // false

Method 2: Array.reduce() Method To Check If All Values In Array Are Equal In JavaScript

The reduce() method applies a function against an accumulator and each element in the array (from left to right) to reduce it to a single value. If you have an array of length 1, the result is that element.

Consider the following example –

var arr = ['a','a','a'];

var allEqual = arr.reduce(function(accumulator, currentValue){ 

  return accumulator && (currentValue === arr[0]? true:false); 

}, true); 

console.log(allEqual); // true 

Here, we have created an array arr of type string and passed it to the reduce() method along with a callback function that checks whether all elements in the array are equal or not by comparing each element with the first element. The output of this code would be –

true // all values in arr are equal i.e., ‘a’ === ‘a’ === ‘a’

You can also use the arrow function syntax as follows –

var arr = ['a','b','c']; 

var allEqual = arr.reduce((accumulator, currentValue) => accumulator && (currentValue === arr[0]), true); 

console.log(allEqual); // false

This method will also return true for an empty array –

var arr = [];

var allEqual = arr.reduce((accumulator, currentValue) => accumulator && (currentValue === arr[0]), true);

console.log(allEqual); // true

This might be fine for your use case, but if you want to return false for an empty array, you can check for that separately in the code –

var arr = []; 

var allEqual = arr.length > 0 && arr.reduce((accumulator, currentValue) => accumulator && (currentValue === arr[0]), true);

console.log(allEqual); // false

Method 3: Use The Set Object To Check If All Values In Array Are Equal In JavaScript

The Set object lets you store unique values of any type, whether primitive values or object references. The size of the Set is determined by the Set.size property.

Consider the following example –

var arr = ['a','b','c'];

var allEqual = (new Set(arr)).size === 1;

console.log(allEqual); // false 

In this code, we have first converted the array to a Set object which stores unique values of the array. Then we have checked the size of Set which should be 1 if all array elements are equal. If not, it should be greater than 1.

var arr = ['a','a','a'];

var allEqual = (new Set(arr)).size === 1;

console.log(allEqual); // true

This method will return false if the array is empty –

var arr = []; 

var allEqual = (new Set(arr)).size === 1;

console.log(allEqual); // false

Conclusion

In this article, we have seen three different methods to check if all values in array are equal in JavaScript. You can choose any of the above-mentioned methods based on your use case and preferences.

I hope this article was helpful to you. If you have any questions, please feel free to post them in the comments section below.

Happy Coding!

Leave a Reply