JavaScript Equality

Beginner
6 min read
Updated: Nov 2026

Definition and Usage

JavaScript has more than one equality algorithm. Daily code should use ===. Object.is treats NaN as equal to NaN. Map and Set key equality uses SameValueZero (like === but NaN equals NaN).

JavaScript Syntax

JavaScript
console.log(0 === -0);
console.log(Object.is(0, -0));
console.log(Number.isNaN(NaN));
console.log(Object.is(NaN, NaN));
Try it Editor

How It Works

  1. ===. +0 and -0 are equal.
  2. Object.is. Distinguishes signed zeros; unifies NaN.

Examples

Example 1 — Avoid ==

Coercion surprises.

JavaScript
console.log("" == false);
console.log("" === false);
Try it Editor

How It Works

  1. == coerces.
  2. === does not.

Example 2 — Map and NaN

NaN works as a Map key.

JavaScript
const m = new Map();
m.set(NaN, "x");
console.log(m.get(NaN));
Try it Editor

How It Works

  1. SameValueZero lets NaN match.

Example 3 — Objects by reference

Different literals differ.

JavaScript
console.log({a:1} === {a:1});
Try it Editor

How It Works

  1. Compare contents manually if needed.

Frequently Asked Questions

Use === for almost everything. Use Object.is when you care about NaN or signed zero.
See the Operators equality page for === vs == examples.

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

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