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)
}
Answer from Rajesh Dixit on Stack Overflow
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(() => [])
🌐
Lodash
lodash.info › doc › isEmpty
isEmpty - Lodash documentation
_.isEmpty(null); // => true _.isEmpty(true); // => true _.isEmpty(1); // => true _.isEmpty([1, 2, 3]); // => false _.isEmpty({ 'a': 1 }); // => false
🌐
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 - 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 ·
🌐
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
🌐
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
🌐
Educative
educative.io › answers › what-is-the-isempty-method-in-lodash
What is the _.isEmpty() method in Lodash?
The _.isEmpty() method in Lodash checks if a value is empty. ... This method returns true if the value is empty, and vice-versa. If the value is a boolean or null, it will always return 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.com › docs
Lodash Documentation
_.isEmpty · _.isEqual · _.isEqualWith · _.isError · _.isFinite · _.isFunction · _.isInteger · _.isLength · _.isMap · _.isMatch · _.isMatchWith · _.isNaN · _.isNative · _.isNil · _.isNull · _.isNumber · _.isObject · _.isObjectLike · _.isPlainObject ·
Find elsewhere
🌐
TutorialsPoint
tutorialspoint.com › lodash › lodash_isempty.htm
Lodash - isEmpty method
var _ = require('lodash'); console.log(_.isEmpty(1)); console.log(_.isEmpty([1, 2, 3])); Save the above program in tester.js. Run the following command to execute this program. \>node tester.js · true false · lodash_lang.htm · Print Page · Previous · Next ·
🌐
Medium
medium.com › @faboulaws › why-you-should-avoid-lodashs-isempty-function-6eb534c147e3
Why You Should Avoid Lodash’s isEmpty Function | by Lod LAWSON-BETUM | Medium
August 13, 2024 - Why You Should Avoid Lodash’s isEmpty Function Lodash’s isEmpty function might seem like a convenient tool for checking if a value is empty, but it has some pitfalls that can lead to unexpected …
🌐
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.
🌐
Tabnine
tabnine.com › home page › code › javascript › lodash
lodash.isEmpty JavaScript and Node.js code examples | Tabnine
get lastChild() { if ... in collection through iteratee. The iteratee is · LoDashStatic.isEmpty · Checks if value is empty....
🌐
CodePen
codepen.io › myogeshchavan97 › pen › PowLwWG
lodash isEmpty Demo
<html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title> </head> <body> <script src="https://cdn.jsdelivr.net/npm/lodash@4.17.15/lodash.min.js"></script> </body> </html> ... const obj1 = { name: 'David' }; console.log('obj1:', _.isEmpty(obj1)); const obj2 = {}; console.log('obj2:', _.isEmpty(obj2)); const array1 = []; console.log('array1:', _.isEmpty(array1)); const array2 = [2, 3]; console.log('array2:', _.isEmpty(array2)); const nullValue = null; console.log('nullValue:', _.isEmpty(nullValue)); const undefinedValue = undefined; console.log('undefinedValue:', _.isEmpty(undefinedValue));
🌐
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
🌐
npm
npmjs.com › package › @types › lodash.isempty
@types/lodash.isempty - npm
// Generated from https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/lodash/scripts/generate-modules.ts import { isEmpty } from "lodash"; export = isEmpty;
      » npm install @types/lodash.isempty
    
🌐
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 ...
🌐
GitHub
gist.github.com › inPhoenix › 45a9f9e2568126d206f1125caebcd122
Alternative to lodash _.isEmpty · GitHub
Alternative to lodash _.isEmpty. GitHub Gist: instantly share code, notes, and snippets.