What is the
every() is an
Array method that checks if all the elements in an array pass a test (implemented by the provided function). The
every() method returns a Boolean value (
false) on each of the elements of on
Check if all the values in the ages array are adults (18 and above):
An array of different ages
const ages = [18, 20, 30, 35, 40];
A function that checks if
ageis 18 and above
const checkAge = (age) => age >= 18;
console.log(ages.every(checkAge)); // expected output: true
If we change the content of ages by including age that is less than
const ages = [18, 20, 30, 35, 40, 15];
console.log(ages.every(checkAge)); // expected output: false
every() method executes the function once for each of the elements present in an array. If it finds an array element where the function returns a
false and does not check the remaining values. If no
every() returns true.
Also, note that the
every() method doesn't change the value of an array.
If you like and appreciate my hours of effort to research, write code, and write this article. Please share your love and reaction ;) 😊. Thanks in advance.