Lodash _.eq() method

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

What you’ll learn

  • How _.eq(value, other) performs a SameValueZero comparison.
  • Why NaN can match NaN here while === does not.
  • How _.eq differs from Object.is around signed zeros.
  • Reference equality rules for objects and arrays.

Prerequisites

Comfort with ===, truthiness, and the idea that NaN !== NaN in JavaScript.

  • You know primitives versus object references.
  • You can open Try-it labs in the browser.

Overview

_.eq wraps SameValueZero semantics in one readable helper. Reach for it when you want predictable comparisons for tricky numeric edge cases without repeating boilerplate.

NaN-friendly

Compare computed floats without custom Number.isNaN branches everywhere.

Zeros aligned

SameValueZero treats 0 and -0 as equal.

Refs unchanged

Objects still compare by identity—deep equality belongs elsewhere.

Syntax

javascript
_.eq(value, other)
  • value: first value to compare.
  • other: second value to compare.
  • Returns: true if SameValueZero holds; otherwise false.
1

Primitives and strings

Same types and values compare as expected; mismatched types usually yield false.

javascript
import eq from "lodash/eq";

eq(42, 42);           // true
eq("hi", "hi");       // true
eq(1, "1");           // false
Try it Yourself
2

NaN and signed zero

SameValueZero fixes NaN comparisons and aligns 0 with -0.

javascript
import eq from "lodash/eq";

eq(NaN, NaN);         // true  (unlike NaN === NaN)
eq(0, -0);            // true  (Object.is disagrees here)
Try it Yourself
3

Object identity

Two different object literals are not equal; only the same reference passes.

javascript
import eq from "lodash/eq";

var a = { x: 1 };
var b = { x: 1 };

eq(a, a);             // true
eq(a, b);             // false
Try it Yourself

📋 _.eq vs === vs Object.is

APINaN vs NaN+0 vs -0
_.eq(a, b) (SameValueZero)EqualEqual
a === bNot equalEqual
Object.is(a, b) (SameValue)EqualNot equal

Pitfalls to avoid

Depth

Expecting deep equality

Use _.isEqual or structured comparison utilities for matching object contents.

Coercion

Loose == habits

_.eq does not coerce types like ==; 1 and "1" are not equal.

Zeros

Need SameValue, not SameValueZero

If you must distinguish +0 and -0, prefer Object.is.

❓ FAQ

No. Strict equality fails for NaN and treats +0 and -0 slightly differently in language discussions; _.eq uses SameValueZero, which matches NaN to NaN and +0 to -0.
Object.is follows SameValue, which distinguishes +0 and -0. _.eq uses SameValueZero, so it treats +0 and -0 as equal.
It compares by reference, like ===. Two different object literals are not equal even if their contents match.
Use it when you want one consistent comparison helper across a codebase, especially for NaN-tolerant checks.

Summary

  • Purpose: SameValueZero comparison in one call.
  • Strength: sane rules for NaN and signed zero.
  • Next: explore more on Lodash _.gt().
Did you know?

SameValueZero is the same rule used for Map and Set key equality: NaN matches NaN, and +0 matches -0.

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