Friday, September 5, 2025
HomeLanguagesJavascriptLodash _.every() Method

Lodash _.every() Method

Lodash _.every() method checks if the predicate returns true for all elements of the collection and iteration is stopped once the predicate returns falsely.

Note: Also this method returns true for empty collections because everything is true for elements of empty collections.

Syntax:

_.every(collection, [predicate=_.identity]);

Parameters:

  • collection (Array|Object): This parameter holds the collection to iterate over.
  • [predicate=_.identity] (Function): This parameter holds the function invoked per iteration.

Return Value:

This method is used to return true if all elements pass the predicate check, else false.

Example 1: In this example, we are checking is every element of the given array has that data type or not and printing the result by the use of the _.every() method.

javascript




// Requiring the lodash library
const _ = require("lodash");
 
// Original array
 
let obj1 = ([true, 1, null, 'yes']);
let obj2 = ([true, 2, 'active', 'yes']);
 
// Use of _.every() method
 
let x = _.every(obj1, Boolean);
let y = _.every(obj2, Boolean);
 
// Printing the output
console.log(x);
console.log(y);


Output:

false
true

Example 2: In this example, we are checking is every element of the given array has that same object or not and printing the result by the use of the _.every() method.

javascript




// Requiring the lodash library
const _ = require("lodash");
 
// Original array
 
let users = [{ 'user': 'jonny', 'age': 30, 'active': false },
{ 'user': 'harry', 'age': 35, 'active': false }];
 
// Use of _.every() method
// The `_.matches` iteratee shorthand.
 
let x = _.every(users, { 'user': 'barney', 'active': false });
 
// Printing the output
console.log(x);


Output:

false

Example 3: In this example, we are checking is every element of the given array of objects has that same field or not and printing the result by the use of the _.every() method.

javascript




// Requiring the lodash library
const _ = require("lodash");
 
// Original array
 
let users = [
    { 'user': 'jonny', 'age': 30, 'active': false },
    { 'user': 'harry', 'age': 35, 'active': false }
];
 
// Use of _.every() method
// The `_.matchesProperty` iteratee shorthand.
 
let x = _.every(users, ['active', false]);
 
// Printing the output
console.log(x);


Output:

true

Example 4: In this example, we are checking is every element of the given array of objects has that string or not and printing the result by the use of the _.every() method.

javascript




// Requiring the lodash library
const _ = require("lodash");
 
// Original array
 
let users = [
    { 'user': 'jonny', 'age': 30, 'active': false },
    { 'user': 'harry', 'age': 35, 'active': false }
];
 
// Use of _.every() method
// The `_.property` iteratee shorthand.
 
let x = _.every(users, 'active');
 
// Printing the output
console.log(x);


Output:

false
Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!
RELATED ARTICLES

Most Popular

Dominic
32269 POSTS0 COMMENTS
Milvus
81 POSTS0 COMMENTS
Nango Kala
6638 POSTS0 COMMENTS
Nicole Veronica
11802 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11866 POSTS0 COMMENTS
Shaida Kate Naidoo
6752 POSTS0 COMMENTS
Ted Musemwa
7027 POSTS0 COMMENTS
Thapelo Manthata
6704 POSTS0 COMMENTS
Umr Jansen
6721 POSTS0 COMMENTS