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
🌐
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 ·
🌐
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
Discussions

lodash isEmpty and has method vs simple check
Note: This does not mean you should use lodash for such purpose. You can write your own isEmpty function. More on stackoverflow.com
🌐 stackoverflow.com
I was doing it wrong for years LOL. lodash/underscore _.isEmpty() actually works differently then I expected.
The real problem here, I think, is that both you and underscore's authors seem to think it's OK for communication about a function's purpose to be as vague as human intuition as to what constitutes a "somehow empty" value. isEmpty may be pretty but it invites errors; isEmptyArray and isEmptyObject and isUndefined don't. More on reddit.com
🌐 r/javascript
3
0
July 4, 2015
_.isEmpty returns false for numbers
console.log(_.isEmpty(123456)); · this returns true, should be false More on github.com
🌐 github.com
5
March 14, 2014
isEmpty returns true when a number is passed in
There was an error while loading. Please reload this page · This doesn't seem right to me More on github.com
🌐 github.com
2
April 7, 2017
🌐
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
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 › @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 …
Find elsewhere
🌐
npm
npmjs.com › package › @types › lodash.isempty
@types/lodash.isempty - npm
November 7, 2023 - TypeScript definitions for lodash.isempty. Latest version: 4.4.9, last published: 2 years ago. Start using @types/lodash.isempty in your project by running `npm i @types/lodash.isempty`. There are 35 other projects in the npm registry using @types/lodash.isempty.
      » npm install @types/lodash.isempty
    
🌐
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
🌐
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
🌐
DEV Community
dev.to › ml318097 › lodash--isempty-check-falsy-values-3d2a
Lodash: _.isEmpty() - Check falsy values - DEV Community
June 21, 2021 - _.isEmpty(): Matches undefined, null, false & empty arrays, obj, strings const _ =... Tagged with lodash, webdev, codenewbie, beginners.
🌐
CodeSandbox
codesandbox.io › examples › package › lodash.isempty
lodash.isempty examples - CodeSandbox
Use this online lodash.isempty playground to view and fork lodash.isempty example apps and templates on CodeSandbox.
🌐
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.
🌐
Cloudsmith
cloudsmith.com › navigator › npm › lodash.isempty
lodash.isempty (4.4.0) - npm Package Quality | Cloudsmith Navigator
Learn all about the quality, security, and current maintenance status of lodash.isempty using Cloudsmith Navigator
🌐
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
🌐
Reddit
reddit.com › r/javascript › i often find myself writing object.keys(someobject) > 0 to test if an object isn't {} (empty) there must be a more beautiful way.
r/javascript on Reddit: I often find myself writing Object.keys(someObject) > 0 to test if an object isn't {} (empty) there must be a more beautiful way.
September 4, 2018 -

Hi everyone,

I very often find myself writing something like

if( Object.keys(someObject).length > 0 ) {

//do some wild stuff

}

To check if a basic object is empty or not i.e. not {} there must be a beautiful way.

I know lodash and jQuery have their solutions, but I don't want to import libraries for a single method, so I'm about to write a function to use across a whole project, but before I do that I want to know I'm not doing something really stupid that ES6/ES7/ES8 can do that I'm just not aware of.

edit solution courtesy of u/vestedfox

Import https://www.npmjs.com/package/lodash.isequal

Total weight added to project after compilation: 355 bytes

Steps to achieve this.

npm i --save lodash.isequal

then somewhere in your code

const isEqual = require('lodash.isequal');

If you're using VueJS and don't want to have to include this in every component and don't want to pollute your global namespace you can do this in app.js

const isEqual = require('lodash.isequal');
Vue.mixin({
  methods: {
    isEqual: isEqual
  }
});

Then in your components you can simply write.

if( this.isEqual(someObject, {})) {
   console.log('This object has properties');
}

🌐
npm
npmjs.com › package › lodash.isempty
lodash.isempty - npm
August 13, 2016 - The lodash method `_.isEmpty` exported as a module.. Latest version: 4.4.0, last published: 10 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
🌐
GitHub
github.com › lodash › lodash › issues › 4598
Provides _.isNotEmpty() would be helpful · Issue #4598 · lodash/lodash
October 30, 2019 - lodash / lodash Public · Notifications · You must be signed in to change notification settings · Fork 7.1k · Star 61.5k · New issueCopy link · New issueCopy link · Closed · Closed · Provides _.isNotEmpty() would be helpful#4598 · Copy link · Labels · question · yeongjet · opened · on Dec 24, 2019 · Issue body actions · That would be more readability than writing !_.isEmpty(), and in some case when pass _.isEmpty as parameter to a function and expect _.isEmpty return a opposite value but you can not process in that function, you can't write !_.isEmpty, it's very inconvenient.
Published   Dec 24, 2019