The Array every() Method in JavaScript

Subscribe to my newsletter and never miss my upcoming articles

What is the every() method in JavaScript?

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 (true or false) on each of the elements of on Array.

Example of every()

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 age is 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 18

Example:

const ages = [18, 20, 30, 35, 40, 15];
console.log(ages.every(checkAge));
// expected output: false

Conclusion:

The 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 value, every() returns false and does not check the remaining values. If no false occur, 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.

Reference:

MDN Web Docs

No Comments Yet