🌐
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

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 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 27, 2017
How to check if object is empty using lodash _isEmpty?
i have map function that is returning empty object for the array now if i check array _isEmpty this condition should satisfy but its not getting into if statement. Any idea what is implemented wron... More on stackoverflow.com
🌐 stackoverflow.com
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
People also ask

Why should I use Lodash's isEmpty method in my React project?
Lodashs isEmpty method is reliable across different types and browsers, and it simplifies code readability.
🌐
dhiwise.com
dhiwise.com › post › react-check-if-object-is-empty-a-simple-guide-for-developers
React Check if Object is Empty: A Simple Guide
Are there any performance considerations when using JSON.stringify() to check if an object is empty?
Yes, `JSON.stringify()` can be slower for large objects due to the string conversion process.
🌐
dhiwise.com
dhiwise.com › post › react-check-if-object-is-empty-a-simple-guide-for-developers
React Check if Object is Empty: A Simple Guide
Can JSON.stringify() handle circular references when checking for empty objects?
No, `JSON.stringify()` can’t handle circular references and will throw an error if it encounters them.
🌐
dhiwise.com
dhiwise.com › post › react-check-if-object-is-empty-a-simple-guide-for-developers
React Check if Object is Empty: A Simple Guide
🌐
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.
🌐
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
🌐
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
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 …
🌐
Qiita
qiita.com › javascript
_.isEmptyの返却値まとめ - Qiita
November 21, 2019 - _.isEmptyの返却値まとめ 検証 lodashの isEmpty 関数に何を入れると何が返却されるかをまとめました。 足りてないと思った方は追記してください。 検証version lodash.js/4.1...
🌐
npm
npmjs.com › package › @types › lodash.isempty
@types/lodash.isempty - npm
November 7, 2023 - // 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
    
🌐
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 › 3216
isEmpty returns true when a number is passed in · Issue #3216 · lodash/lodash
April 27, 2017 - _.isEmpty(1234) is true _.isEmpty('1234') is false This doesn't seem right to me.
Published   Jun 22, 2017
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(() => [])
🌐
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
August 13, 2016 - The lodash method _.isEmpty exported as a Node.js module.
🌐
GitHub
github.com › lodash › lodash › issues › 4598
Provides _.isNotEmpty() would be helpful · Issue #4598 · lodash/lodash
December 24, 2019 - lodash / lodash Public · Notifications · You must be signed in to change notification settings · Fork 7.1k · Star 61.4k · 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
🌐
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