Lodash _.isElement() method
What you’ll learn
- How
_.isElement(value)narrows unknown values to DOM element nodes. - Why adjacent structures—text nodes, comments, documents—fail the check.
- How lodash blocks forged plain objects even when
nodeTypeis spoofed. - When to pair lodash with framework refs instead of hand-built guards.
Prerequisites
Browser DOM basics: elements versus text nodes, and access to document when running the samples.
- You know element nodes participate in the DOM tree (not every object returned by APIs is an element).
- Try-it labs run in a browser context with lodash loaded from the CDN.
Overview
Reach for _.isElement when sanitizing unknown DOM references, guarding drag-and-drop payloads, or validating callbacks that should receive real elements—not serialized JSON blobs.
Element nodes
Positive when nodeType === 1 inside an object-like host.
Not text or comments
Adjacent node kinds fail fast—use narrower helpers if you need them.
Plain-object guard
Rejects forged literals even when nodeType is mimicked.
Syntax
_.isElement(value) - value: any value to test.
- Returns:
trueifvaluelooks like a DOM element node; otherwisefalse.
Fresh elements from document.createElement
Even before insertion into the document tree, constructed tags register as element nodes.
import isElement from "lodash/isElement";
console.log(
"createdDiv: " + isElement(document.createElement("div")) + "\n" + // true
"createdSpan: " + isElement(document.createElement("span")) // true
); Text nodes, spoofed objects, and null
Other node kinds and plain-object literals do not satisfy lodash even when they resemble DOM hosts.
import isElement from "lodash/isElement";
console.log(
"textNode: " + isElement(document.createTextNode("hi")) + "\n" + // false
"fakePlain: " + isElement({ nodeType: 1 }) + "\n" + // false
"nullish: " + isElement(null) // false
); Comments versus SVG elements
Comments stay non-elements; namespace-created SVG roots remain elements.
import isElement from "lodash/isElement";
console.log(
"commentNd: " + isElement(document.createComment("")) + "\n" + // false
"svgElem: " + isElement(document.createElementNS("http://www.w3.org/2000/svg", "svg")) // true
); 📋 _.isElement vs related checks
| API | Matches |
|---|---|
_.isElement(x) | DOM nodes whose nodeType === 1 (elements), excluding lodash plain objects. |
x.nodeType === 1 | Low-level equivalent—lodash layers plain-object filtering. |
x instanceof HTMLElement | HTML-only; SVG math nodes may fail despite being elements. |
_.isPlainObject(x) | Opposite axis—serialization payloads rather than live DOM nodes. |
Pitfalls to avoid
Missing document
Server bundles crash if snippets reference document unconditionally—gate DOM code behind runtime detection.
Framework wrappers
React/Vue refs might expose components or arrays—unwrap to native nodes before calling lodash.
Document fragments
DocumentFragment uses another nodeType—treat separately from element validation.
❓ FAQ
Summary
- Purpose: confirm unknown values are DOM element nodes inside lodash-heavy utilities.
- Exclude: text nodes, comments, forged plain objects, and non-element hosts.
- Next: explore more on Lodash _.isEmpty().
_.isElement returns true only when the value is object-like, has nodeType === 1 (an element node), and is not a lodash plain object—so a forged { nodeType: 1 } literal still fails.
6 people found this page helpful
