lodash isEmpty and has method vs simple check lodash isEmpty and has method vs simple check reactjs reactjs

lodash isEmpty and has method vs simple check


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 variable and not a datastructure 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.