To check if all values in an array are equal in JavaScript, you can use the every() method in combination with the === operator to compare each element of the array to the first element, like this:
function allEqual(arr) {
return arr.every(val => val === arr[0]);
}
This function takes an array arr as its parameter and returns true if all values in the array are equal, and false otherwise.
Here's an example usage:
const arr1 = [1, 1, 1, 1];
const arr2 = [1, 2, 3, 4];
const arr3 = ['foo', 'foo', 'foo'];
console.log(allEqual(arr1)); // true
console.log(allEqual(arr2)); // false
console.log(allEqual(arr3)); // true
In the example above, arr1 and arr3 contain only equal values, so the allEqual() function returns true for both of them. arr2 contains different values, so the function returns false.