Lodash _.isEmpty() method
What you’ll learn
- How
_.isEmpty(value)treats arrays, strings, buffers, and array-like values. - When plain objects and
Map/Setinstances count as empty. - Why
null/undefinedand many primitives read as empty. - Where lodash differs from rolling your own
Object.keyschecks.
Prerequisites
Comfort with JavaScript collections (arrays and strings) and plain objects.
- You understand
lengthfor arrays/strings versus keyed properties on objects. - Try-it labs load lodash from the CDN.
Overview
Use _.isEmpty before rendering placeholders, enabling submit buttons, or pruning caches—anywhere “no meaningful payload yet” must be detected uniformly across arrays, maps, and POJOs.
Collections
Arrays, strings, typed buffers—lodash reads length where applicable.
Keyed data
Plain objects lose emptiness as soon as an own enumerable key appears.
Map / Set
Native maps and sets compare size against zero.
Syntax
_.isEmpty(value) - value: any value to test.
- Returns:
trueifvalueis empty per lodash rules; otherwisefalse.
Arrays and strings
Zero-length collections register as empty; a populated array fails the check.
import isEmpty from "lodash/isEmpty";
console.log(
"emptyArr: " + isEmpty([]) + "\n" + // true
"emptyStr: " + isEmpty("") + "\n" + // true
"nonEmptyArr: " + isEmpty([1]) // false
); null and non-collection primitives
Lodash treats nullish values as empty; numbers and booleans fall outside collection branches and read empty too.
import isEmpty from "lodash/isEmpty";
console.log(
"nullVal: " + isEmpty(null) + "\n" + // true
"primitiveNum: " + isEmpty(1) + "\n" + // true
"primitiveBool: " + isEmpty(false) // true
); Plain objects and Map
Keyed payloads flip false once data exists; empty maps clear via size.
import isEmpty from "lodash/isEmpty";
console.log(
"emptyPlain: " + isEmpty({}) + "\n" + // true
"hasKeys: " + isEmpty({ a: 1 }) + "\n" + // false
"emptyMap: " + isEmpty(new Map()) // true
); 📋 _.isEmpty vs related checks
| API | Matches |
|---|---|
_.isEmpty(x) | Structural emptiness—collections without entries or objects without own enumerable keys. |
_.isNil(x) | Only null or undefined; ignores empty arrays/strings. |
(x || []).length === 0 | Array/string-specific hack—misses objects/maps unless duplicated logic. |
Object.keys(x).length === 0 | Plain-object oriented—throws or misbehaves on nullish values without guards. |
Pitfalls to avoid
Strings with spaces
" " is non-empty—trim first if whitespace should collapse to empty.
Primitives aren’t “full”
Numbers returning true surprises newcomers—pair with _.isNumber when distinguishing missing payloads.
NodeLists
Array-like DOM collections follow length; an attached-but-empty NodeList is still empty.
❓ FAQ
Summary
- Purpose: unify emptiness checks across arrays, strings, keyed objects, maps, and sets.
- Remember: nullish and many primitives report empty—do not equate with user-visible “missing form data.”
- Next: explore more on Lodash _.isEqual().
_.isEmpty returns true for null and undefined, zero-length arrays and strings, objects with no own enumerable keys (via iteration), and Map/Set instances whose size is 0. Many primitives—such as numbers or booleans—are treated as empty because they are not collections.
6 people found this page helpful
