JavaScript - Check if All Array Values Are Equal
Last Updated :
23 Jan, 2025
Here are the different methods to check if all values of an array are equal or not in JavaScript
1. Using Array.every() method
First, get the array of elements. Pass it to an arrow function, which calls every() method on each array element and returns true if each element matches the first element of the array.
JavaScript
let a = [1, 1, 1, 1];
const all =
a => a.every(v => v === a[0]);
console.log(all(a));
In this example
- The all() function checks if all elements in the array are equal to the first element using the every() method.
- In this case, it returns true because all elements in [1, 1, 1, 1] are equal to the first element (1)
2. Using Array.reduce() method
First, get the array of elements. Pass it to a function called the reduce() method on the array element. Return true if each element matches the first element of the array.
JavaScript
let a = ["G", "F", "I", "J"];
function all(a) {
if (!a.length) return true;
return a.reduce(function (a, b) {
return (a === b) ? a : (!b);
}) === a[0];
}
console.log(all(a));
In this example:
- The all() function uses reduce() to compare all elements of the array a with each other and checks if they are all equal.
- It returns false because the elements in a (["G", "F", "I", "J"]) are not all equal.
3. Using Set
To check if all elements in an array are equal, we can pass the array to the Set constructor. Since a Set only stores unique elements, if the size of the set is 1, it indicates that all elements in the array are equal.
JavaScript
let a = [1, 1, 1, 1];
function all(a) {
const res = new Set(a).size === 1;
return res;
}
console.log(all(a));
In this example
- The all() function converts the array into a Set, which removes duplicate elements, and checks if the set's size is 1.
- It returns true because all elements in the array [1, 1, 1, 1] are the same, so the set contains only one unique element.
4. Using for...of loop
The for...of loop iterates over the iterable objects (like Array, Map, Set, arguments object, …,etc) and will check the element is same as the first element.
JavaScript
const a1 = [1, 1, 1, 1];
const a2 = [1, 1, 2];
function all(a) {
let eq = true;
for (const element of a) {
if (element !== a[0]) {
eq = false;
break;
}
}
return eq;
}
console.log(all(a1));
console.log(all(a2));
In this example
- The all() function checks if all elements in the array a are equal to the first element.
- It loops through each element in a and compares it with the first element (a[0]).
- If any element is different, it sets eq to false and breaks the loop.
- The function returns true if all elements are the same and false otherwise.
5. Using Array.some() Method
The Array.some() method to determine if any element in the array does not match the first element. If the method returns false, it means all elements are equal.
JavaScript
function elem(a) {
if (a.length === 0) return true;
const b = a[0];
return !a.some(element => element !== b);
}
const a1 = [1, 1, 1, 1];
const a2 = [1, 2, 1, 1];
console.log(elem(a1));
console.log(elem(a2));
In this example
- The elem() function checks if all elements in the array arr are equal to the first element by using some() to find any element not equal to the first one.
- It returns true for arr1 (all elements are equal) and false for arr2 (not all elements are equal).
Similar Reads
JavaScript- Arrays are Equal or Not These are the following approaches to compare two arrays in JavaScript:1. Using the JSON.stringify() MethodJavaScript provides a function JSON.stringify() method in order to convert an object whether or array into a JSON string. By converting it into JSON strings, we can directly check if the string
4 min read
How to Check all values of an array are equal or not in PHP? Given an array with some elements, the task is to check whether all values of array elements are equal or not. Below are the approaches to check if all values of an array are equal or not in PHP: Table of Content Using array_unique() FunctionUsing array_reduce() FunctionUsing array_filter() Function
4 min read
JavaScript Program to Check if Two Arrays are Equal or Not Given two arrays, arr1 and arr2 of equal length N, the task is to find if the given arrays are equal or not. Two arrays are said to be equal if: Both of them contain the same set of elements, Arrangements (or permutations) of elements might/might not be the same.If there are repetitions, then counts
4 min read
JavaScript Program to Check if an Array Contains only Unique Values In this article, we are given an array, Our task is to find whether the elements in an array are unique or not.Examples:Input 1: 7,8,1,5,9 Output: true Input2: 7,8,1,5,5 Output: falseIn Input 1, elements 7,8,1,5,9 are distinct from each other and they were unique, there was no repetition of elements
4 min read
Java Program to Compare two Boolean Arrays Two arrays are equal if they contain the same elements in the same order. In java, we can compare two Boolean Arrays in 2 ways: By using Java built-in method that is .equals() method.By using the Naive approach. Examples: Input : A = [true , true , false] A1 = [true, true, false] Output: Both the ar
3 min read