🌐
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
🌐
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 - function isEmptyValues(value) { return value === undefined || value === null || value === NaN || (typeof value === 'object' && Object.keys(value).length === 0 || (typeof value === 'string' && value.trim().length() === 0; Credit to https://stackoverflow.com/a/43233163/10265299 https://stackoverflow.com/users/3783478/rajesh for original function. I just add the check of NaN. JavaScript · Lodash ·
🌐
Lodash
lodash.info › doc › isEmpty
isEmpty - Lodash documentation
Array-like values such as arguments objects, arrays, buffers, strings, or jQuery-like collections are considered empty if they have a length of 0. Similarly, maps and sets are considered empty if they have a size of 0. _.isEmpty(null); // => ...
🌐
TutorialsPoint
tutorialspoint.com › lodash › lodash_isempty.htm
Lodash - isEmpty method
Checks if value is an empty object, collection, map, or set.
🌐
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.
🌐
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
🌐
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
Top answer
1 of 16
5110

Empty string, undefined, null, ...

To check for a truthy value:

if (strValue) {
    // strValue was non-empty string, true, 42, Infinity, [], ...
}

To check for a falsy value:

if (!strValue) {
    // strValue was empty string, false, 0, null, undefined, ...
}

Empty string (only!)

To check for exactly an empty string, compare for strict equality against "" using the === operator:

if (strValue === "") {
    // strValue was empty string
}

To check for not an empty string strictly, use the !== operator:

if (strValue !== "") {
    // strValue was not an empty string
}
2 of 16
1446

For checking if a variable is falsey or if it has length attribute equal to zero (which for a string, means it is empty), I use:

function isEmpty(str) {
    return (!str || str.length === 0 );
}

(Note that strings aren't the only variables with a length attribute, arrays have them as well, for example.)

Alternativaly, you can use the (not so) newly optional chaining and arrow functions to simplify:

const isEmpty = (str) => (!str?.length);

It will check the length, returning undefined in case of a nullish value, without throwing an error. In the case of an empty value, zero is falsy and the result is still valid.

For checking if a variable is falsey or if the string only contains whitespace or is empty, I use:

function isBlank(str) {
    return (!str || /^\s*$/.test(str));
}

If you want, you can monkey-patch the String prototype like this:

String.prototype.isEmpty = function() {
    // This doesn't work the same way as the isEmpty function used 
    // in the first example, it will return true for strings containing only whitespace
    return (this.length === 0 || !this.trim());
};
console.log("example".isEmpty());

Note that monkey-patching built-in types are controversial, as it can break code that depends on the existing structure of built-in types, for whatever reason.

🌐
Es-toolkit
es-toolkit.dev › reference › compat › predicate › isEmpty.html
isEmpty (Lodash Compatibility) | es-toolkit
import { isEmpty } from 'es-toolkit/compat'; // String checks isEmpty(''); // true isEmpty('hello'); // false // Array checks isEmpty([]); // true isEmpty([1, 2, 3]); // false // Object checks isEmpty({}); // true isEmpty({ a: 1 }); // false // Map and Set checks isEmpty(new Map()); // true isEmpty(new Set()); // true isEmpty(new Map([['key', 'value']])); // false isEmpty(new Set([1, 2, 3])); // false // null and undefined isEmpty(null); // true isEmpty(undefined); // true isEmpty(); // true // Array-like objects isEmpty({ 0: 'a', length: 1 }); // false isEmpty({ length: 0 }); // false
🌐
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
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(() => [])
🌐
MeasureThat
measurethat.net › Benchmarks › Show › 18286 › 0 › lodash-isempty-vs-native-for-empty-strings
Benchmark: lodash isEmpty vs native for empty strings - MeasureThat.net
Lodash isEmpty vs native .isArray + length · Lodash.js vs Native - empty · Lodash isEmpty vs Native Javascript · Lodash isEmpty vs Native Javascript, many keys · Comments · × · Do you really want to delete benchmark? Cancel Delete · × · FAQ: FAQ · Source code: GitHub/MeasureThat.net ·
🌐
Tabnine
tabnine.com › home page › code › javascript › lodash
lodash.isEmpty JavaScript and Node.js code examples | Tabnine
LoDashStatic.isEmpty · Checks if value is empty. A value is considered empty unless it’s an arguments object, array, string · LoDashStatic.forEach · Iterates over elements of collection invoking iteratee for each element.
🌐
GitHub
github.com › DefinitelyTyped › DefinitelyTyped › issues › 45521
[@types/lodash] !isEmpty should tell typescript variable is not null · Issue #45521 · DefinitelyTyped/DefinitelyTyped
June 16, 2020 - I asked on the typescript discord and was referred here. When using lodash methods like isEmpty which guard against falsey values, typescript still warns/complains of variables which could be null or undefined. The type definitions shoul...
Published   Jun 16, 2020
🌐
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.
🌐
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.