How to Check if all array elements are equal to a given value in Javascript

The every() method tests whether all elements in the array by the provided function. It returns a Boolean value.

const isEqual = (arr, value) => arr.every((item) => item === value);
// isEqual(['1', '1'], '1') === true
// isEqual(['1', '2'], '1') === false

when you want for example to Check if all items in an array are equal. you can compare all items the first item of the array

const ItemsareEqual = (arr) => arr.every((item) => item === arr[0]);
// isEqual(['1', '1']) === true
// isEqual(['1', '2']) === false

Maher Gallaoui

You like our Content? Follow us to stay up-to-date.