. Advertisement .
..3..
. Advertisement .
..4..
Follow the instructions and examples below to learn how to check if all values in array are equal in JavaScript.
How To Check If All Values In Array Are Equal In JavaScript
With Array.every()
The every() method of an Array object tests if all of its elements can pass the provided test function. Its returned values are Boolean: true or false.
There are many syntax variants of this method. But to keep things simple, you can use an inline callback function with every():
every(function(element) { /* … */ })
In this, the element parameter is the current element the every() method is processing.
You can also use the normal callback function syntax:
every(callbackFn)
The callbackFn parameter is the test function you want to apply to every element of the array. The every() method will pass two arguments to it: element (with the same meaning as above) and index (the index of the current element the method is processing).
If your test function always returns a truthy value for every element, the every() method returns true. Even when there is one falsy returned value, false is returned.
The following example shows you can use this method to check if all elements in an array are equal:
const arr1 = ['ITTutoria', 'ITTutoria', 'ITTutoria', 'ITTutoria'];
const arr2 = ['ITTutoria', 'ITTutoria', 'ITTutoria', 'Stack Overflow'];
const arr3 = ['1', '1', 1, '1'];
function check(arr) {
var firstElement = arr[0];
var result = arr.every(
function(element) {
return element === firstElement;
});
return result;
}
console.log(check(arr1));
console.log(check(arr2));
console.log(check(arr3));
Output:
true
false
false
In the test function, we give to the every() method, the current element is compared to the first element. If every element in the array has the same value, this comparison must always return true. Normal rules for comparing values in JavaScript apply here.
Note: follow this guide to get the last element instead.
With Array.filter()
Array.filter() is another method that runs through every element of an array and performs a provided test. But unlike every(), which returns a Boolean value to represent the results, it creates a shallow copy of the array and removes elements that don’t pass the test.
You can use the filter() method with an arrow function for our purpose:
filter((element, index, array) => { /* … */ } )
Parameters:
- element: the current element that filter() is processing
- index: the index of the above element
- array: the array you want to call filter() on
The method will return a shallow copy of the array with only elements that pass the test function. If none of them does, it returns an empty array.
This is how you can rewrite your program with Array.filter():
const arr1 = ['ITTutoria', 'ITTutoria', 'ITTutoria', 'ITTutoria'];
const arr2 = ['ITTutoria', 'ITTutoria', 'ITTutoria', 'Stack Overflow'];
const arr3 = ['1', '1', 1, '1'];
function check(arr) {
var returned_arr = arr.filter((element, index, array) => element === array[0])
var result = returned_arr.length === arr.length;
return result;
}
console.log(check(arr1));
console.log(check(arr2));
console.log(check(arr3));
Output:
true
false
false
The snippet tells the filter() method to compare every element of the arr array with the first element. If all of them have the same value, the returned array should have the same length as the original array. This means every element has passed the test successfully and got to appear in the returned array.
Summary
As this guide has shown you how to check if all values in array are equal in JavaScript, there are multiple solutions, including the every() and filter() methods. They both perform a test on every element of an array, which you can define using a function.
Leave a comment