Lodash _.isElement() method

Beginner
⏱️ 6 min read
📚 Updated: May 2026
🎯 3 Code examples
🚀 3 Try-it labs
Lodash

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 nodeType is 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

javascript
_.isElement(value)
  • value: any value to test.
  • Returns: true if value looks like a DOM element node; otherwise false.
1

Fresh elements from document.createElement

Even before insertion into the document tree, constructed tags register as element nodes.

javascript
import isElement from "lodash/isElement";

console.log(
  "createdDiv: " + isElement(document.createElement("div")) + "\n" + // true
  "createdSpan: " + isElement(document.createElement("span"))       // true
);
Try it Yourself
2

Text nodes, spoofed objects, and null

Other node kinds and plain-object literals do not satisfy lodash even when they resemble DOM hosts.

javascript
import isElement from "lodash/isElement";

console.log(
  "textNode: " + isElement(document.createTextNode("hi")) + "\n" + // false
  "fakePlain: " + isElement({ nodeType: 1 }) + "\n" +               // false
  "nullish: " + isElement(null)                                     // false
);
Try it Yourself
3

Comments versus SVG elements

Comments stay non-elements; namespace-created SVG roots remain elements.

javascript
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
);
Try it Yourself

📋 _.isElement vs related checks

APIMatches
_.isElement(x)DOM nodes whose nodeType === 1 (elements), excluding lodash plain objects.
x.nodeType === 1Low-level equivalent—lodash layers plain-object filtering.
x instanceof HTMLElementHTML-only; SVG math nodes may fail despite being elements.
_.isPlainObject(x)Opposite axis—serialization payloads rather than live DOM nodes.

Pitfalls to avoid

SSR

Missing document

Server bundles crash if snippets reference document unconditionally—gate DOM code behind runtime detection.

Refs

Framework wrappers

React/Vue refs might expose components or arrays—unwrap to native nodes before calling lodash.

Fragments

Document fragments

DocumentFragment uses another nodeType—treat separately from element validation.

❓ FAQ

No. Those use other nodeType values (for example 3 for text, 8 for comments). _.isElement targets element nodes with nodeType 1.
No. Lodash also rejects values classified as plain objects, which blocks simple object literals from impersonating DOM nodes.
Yes. SVG elements created in the DOM are still element nodes with nodeType 1—the helper is not limited to HTML tag names.
Not as written—you need document APIs or a DOM shim such as jsdom. Without that, stick to structural checks that do not reference document.

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().
Did you know?

_.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.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

6 people found this page helpful