_.isEmpty(obj, true)

var obj = {
  'firstName': undefined
, 'lastName' : undefined
};

console.log(_.isEmpty(obj)); // false
console.log(_.isEmpty({})); // true
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.js"></script>

Please, see http://www.ericfeminella.com/blog/2012/08/18/determining-if-an-object-is-empty-with-underscore-lo-dash/

Answer from Armen Zakaryan on Stack Overflow
🌐
Medium
medium.com › @trmaphi › lodash-isempty-value-you-might-be-using-it-the-wrong-way-d83210d7decf
Lodash _.isEmpty(value), you might be using it the wrong way. | by Truong Ma Phi | Medium
July 28, 2019 - I didn’t know it is different between primitive values and object/collection types in JavaScript for empty checking. Look at the default examples on lodash and the extra I added. _.isEmpty(null);// => true_.isEmpty(true);// => true_.isEmpty(1);// => true_.isEmpty([1, 2, 3]);// => false_.isEmpty({ 'a': 1 });// => false_.isEmpty('');// => true_.isEmpty(NaN);// => true_.isEmpty(undefined);// => true ·
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › lodash-_-isempty-method
Lodash _.isEmpty() Method - GeeksforGeeks
Lodash _.isEmpty() method checks if the value is an empty object, collection, map, or set. Objects are considered empty if they have no own enumerable string keyed properties. Collections are considered empty if they have a 0 length.
Published   January 9, 2025
🌐
Lodash
lodash.info › doc › isEmpty
isEmpty - Lodash documentation
Objects are considered empty if they have no own enumerable string keyed properties.
🌐
Docs-lodash
docs-lodash.com › v4 › is-empty
_.isEmpty – Lodash Docs v4.17.11
Array-like values such as arguments objects, arrays, buffers, strings, or jQuery-like collections are considered empty if they have a length of 0. Similarly, maps and sets are considered empty if they have a size of 0. 0.1.0 · value (*): The value to check. (boolean): Returns true if value ...
🌐
npm
npmjs.com › package › lodash.isempty
lodash.isempty - npm
The lodash method `_.isEmpty` exported as a module.. Latest version: 4.4.0, last published: 9 years ago. Start using lodash.isempty in your project by running `npm i lodash.isempty`. There are 1259 other projects in the npm registry using lodash.isempty.
      » npm install lodash.isempty
    
Published   Aug 13, 2016
Version   4.4.0
Author   John-David Dalton
🌐
Lodash
lodash.com › docs
Lodash Documentation
Creates an object composed of keys generated from the results of running each element of collection thru iteratee. The corresponding value of each key is the last element responsible for generating the key. The iteratee is invoked with one argument: (value). ... Creates an array of values by ...
Find elsewhere
🌐
GitHub
github.com › lodash › lodash › issues › 5690
The isEmpty method is not very rigorous in judging empty objects. · Issue #5690 · lodash/lodash
August 18, 2023 - The isEmpty method is not very rigorous in judging empty objects. const obj = { [Symbol('a')]: 1 } when I call _.isEmpty(obj), it return true; Have you considered using Reflect.ownKeys() to solve this problem?
Published   Aug 18, 2023
🌐
Dustin John Pfister
dustinpfister.github.io › 2019 › 09 › 01 › lodash_isempty
The lodash is empty object method for finding out if an object is empty or not | Dustin John Pfister at github pages
November 23, 2020 - In lodash there is the _.isEmpty method than can be used to find if a collection object is empty or not. This is not to be confused with other possible values that might be considered empty such as null, a false boolean value and so forth.
Top answer
1 of 2
39

if(user) will pass for empty Object/Array, but they are empty and should be rejected.

Also if(user) will fail for values like 0 or false which are totally valid values.

Using isEmpty() will take care of such values. Also, it makes code more readable.

Point to note is isEmpty(1) will return true as 1 is a primitive value and not a data structure and hence should return true.

This has been stated in Docs:

Checks if value is an empty object, collection, map, or set.

Also as per docs,

Objects are considered empty if they have no own enumerable string keyed properties.

So if you have an object which does not have non-enumerable properties, its considered as empty. In the below example, foo is a part of object o and is accessible using o.foo but since its non-enumerable, its considered as empty as even for..in would ignore it.

var o = Object.create(null);

Object.defineProperty(o, "foo", {
  enumerable: false,
  value: "Hello World"
})
Object.defineProperty(o, "bar", {
  enumerable: false,
  value: "Testing 123"
});


console.log(o)
for (var k in o) {
  console.log(k)
}

console.log(o.foo)

console.log(_.isEmpty(o))
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.min.js"></script>


Note: This does not mean you should use lodash for such purpose. You can write your own isEmpty function.

Following is something that I use:

This will return true for following cases:

  • {}, [], "", undefined, null, object in above snippet(no enumerable property)
function isEmpty(value){
  return  value === undefined ||
          value === null ||
          (typeof value === "object" && Object.keys(value).length === 0) ||
          (typeof value === "string" && value.trim().length === 0)
}
2 of 2
5

Simple and elegant function for checking the values are empty or not

    function isEmpty(value) {
     const type = typeof value;
     if ((value !== null && type === 'object') || type === 'function') {
       const props = Object.keys(value);
        if (props.length === 0 || props.size === 0) { 
          return true;
        } 
      } 
      return !value;
}

Testing the above method

It will return 'true' for all cases below

console.log(isEmtpy(null)) 
console.log(isEmtpy(0))
console.log(isEmtpy({}))
console.log(isEmtpy(new Set())
console.log(isEmtpy(Object.create(null))
console.log(isEmtpy(''))
console.log(isEmtpy(() => {}))
console.log(isEmtpy(() => [])
🌐
DhiWise
dhiwise.com › post › react-check-if-object-is-empty-a-simple-guide-for-developers
React Check if Object is Empty: A Simple Guide
November 6, 2024 - Lodash’s isEmpty method verifies if objects, arrays, maps, or sets are empty, offering a comprehensive check.
🌐
TutorialsPoint
tutorialspoint.com › lodash › lodash_isempty.htm
Lodash - isEmpty method
Checks if value is an empty object, collection, map, or set.
🌐
GitHub
github.com › lodash › lodash › issues › 5131
isEmpty(new Date) returns true instead of false · Issue #5131 · lodash/lodash
April 6, 2021 - _.isEmpty(new Date) Actual returns: true Expected: false
Published   Apr 06, 2021
🌐
GitHub
github.com › lodash › lodash › issues › 496
_.isEmpty returns false for numbers · Issue #496 · lodash/lodash
March 14, 2014 - console.log(_.isEmpty(123456)); this returns true, should be false.
Published   Mar 14, 2014
🌐
Es-toolkit
es-toolkit.dev › reference › compat › predicate › isEmpty.html
isEmpty (Lodash Compatibility) | es-toolkit
import { isEmpty } from 'es-toolkit/compat'; // String checks isEmpty(''); // true isEmpty('hello'); // false // Array checks isEmpty([]); // true isEmpty([1, 2, 3]); // false // Object checks isEmpty({}); // true isEmpty({ a: 1 }); // false // Map and Set checks isEmpty(new Map()); // true isEmpty(new Set()); // true isEmpty(new Map([['key', 'value']])); // false isEmpty(new Set([1, 2, 3])); // false // null and undefined isEmpty(null); // true isEmpty(undefined); // true isEmpty(); // true // Array-like objects isEmpty({ 0: 'a', length: 1 }); // false isEmpty({ length: 0 }); // false
🌐
Tabnine
tabnine.com › home page › code › javascript › lodash
lodash.isEmpty JavaScript and Node.js code examples | Tabnine
LoDashStatic.isEmpty · Checks if value is empty. A value is considered empty unless it’s an arguments object, array, string · LoDashStatic.forEach · Iterates over elements of collection invoking iteratee for each element.