14

I can do if(user) { //do something } but I've seen people used lodash to do simple checking. I've read lodash's docs about isEmpty and has methods, but can't figure out why you'd want to use these.

import { isEmpty } from 'lodash'; 

if(isEmpty(user)) { //omg why? }

2 Answers 2

34

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)
}
9
  • 2
    It's just fine to pass 0 or false, why not?
    – Jenny Mok
    Apr 5, 2017 at 14:32
  • Point is when you do if(user) its read as if user exists then... Now false or 0 are valid values, but JS handles it differently. So when you write _.isEmpty(variable) a normal person would read it as if variable is empty then..., but a known user would deduce that variable is of type object and should have some enumerable property and that makes things really easy to understand.
    – Rajesh
    Apr 5, 2017 at 14:57
  • Object.keys([null, null, null]) === ["0", "1", "2"]
    – MrYellow
    Jun 29, 2021 at 0:13
  • 1
    @Yarin Thank you for the credit. However, I would not suggest using NaN check for this. NaN is a value which is not empty. Its just used incorrectly. You should create another function isValid which can have that check. But once again, thank you for the credit. Means a lot to me
    – Rajesh
    Dec 8, 2022 at 6:45
  • 1
    @Rajesh good point. I think your function is better anyway
    – Yarin
    Dec 8, 2022 at 15:02
3

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(() => [])
1
  • isEmpty([null, null, null]) === false
    – MrYellow
    Jun 29, 2021 at 0:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.