JavaScript === and ==

Beginner
5 min read
Updated: Nov 2026
3 examples

Definition and Usage

Use === (strict) to compare value and type with no coercion. == (loose) converts types before comparing and surprises beginners—prefer === daily.

JavaScript Syntax

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

How It Works

  1. === keeps types. number vs string is false.
  2. == coerces. "1" becomes 1.
  3. NaN. Never equals itself; use Number.isNaN.

Examples

Example 1 — null and undefined

Loose equality treats them as equal.

JavaScript
console.log(null == undefined);
console.log(null === undefined);
Try it Editor

How It Works

  1. == special-cases null/undefined.
  2. === keeps them different.

Example 2 — Objects compare by reference

Different objects are not ===.

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

How It Works

  1. Two literals are different references.
  2. Same variable matches.

Example 3 — Object.is

Like === but Object.is(NaN, NaN) is true.

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

How It Works

  1. Object.is handles NaN and signed zero differently from ===.

Frequently Asked Questions

Prefer === and !== unless you intentionally want coercion.
Number.isNaN(value).

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