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
🌐
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
🌐
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
🌐
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
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(() => [])
🌐
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 · This look like it doesn’t work on boolean and number.
🌐
GitHub
github.com › lodash › lodash › issues › 3216
isEmpty returns true when a number is passed in · Issue #3216 · lodash/lodash
April 7, 2017 - _.isEmpty(1234) is true _.isEmpty('1234') is false This doesn't seem right to me.
Published   Jun 22, 2017
🌐
Docs-lodash
docs-lodash.com › v4 › is-empty
_.isEmpty – Lodash Docs v4.17.11
_.isEmpty(null); // => true _.isEmpty(true); // => true _.isEmpty(1); // => true _.isEmpty([1, 2, 3]); // => false _.isEmpty({ 'a': 1 }); // => false
🌐
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
Find elsewhere
🌐
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.
🌐
TutorialsPoint
tutorialspoint.com › lodash › lodash_isempty.htm
Lodash - isEmpty method
Checks if value is an empty object, collection, map, or set.
🌐
Makandra
makandracards.com › makandra › 29119-lodash-isblank-ispresent-mixins
LoDash: isBlank and isPresent mixins - makandra dev
When you need to check a value for presence, don't rely on JavaScript since it considers 0 or "0" false. Also don't rely on LoDash's _.isEmpty: if ('0') { ... } // false if (0) { ... } // false ^ i...
🌐
GitHub
github.com › jashkenas › underscore › issues › 432
_.isEmpty Behavior on numbers · Issue #432 · jashkenas/underscore
December 8, 2011 - var cow = {a: 1, b: {}, c: []}; _.isEmpty(cow); // returns false isEmpty works fine on objects containing numbers as the only 'non-empty' values _.isEmpty(cow.a); // returns true However the individual k,v of a:1 is considered empty. I d...
Published   Jan 13, 2012
🌐
Lodash
lodash.com › docs
Lodash Documentation
Note: This method is loosely based on the structured clone algorithm and supports cloning arrays, array buffers, booleans, date objects, maps, numbers, Object objects, regexes, sets, strings, symbols, and typed arrays. The own enumerable properties of arguments objects are cloned as plain objects.