Skip to main content
⚑ OpenDevDocs
JavaScript
programmingjavascriptGuideBeginner

JavaScript Operators & Strict Equality

Mastering JavaScript operators: arithmetic, logical (&&, ||, ??), comparison (== vs ===), optional chaining (?.), and ternary expressions.

Verified: 2026-09-30
Tested on:node 22.x
Platforms:
webnode
#javascript#operators#equality#nullish-coalescing#optional-chaining

JavaScript Operators & Strict Equality

Operators perform mathematical calculations, string concatenations, logical evaluations, and conditional assignments.


1. Strict (===) vs. Loose (==) Equality

Always use Strict Equality (===). Loose equality (==) performs unpredictable type coercion:

// ❌ Loose Equality (Performs Coercion)
0 == false;        // true
"" == false;       // true
null == undefined; // true
"42" == 42;        // true

// βœ… Strict Equality (Compares Type AND Value)
0 === false;        // false
"" === false;       // false
null === undefined; // false
"42" === 42;        // false

2. Logical Operators & Short-Circuit Evaluation

OperatorNameShort-Circuit RuleExample
&&Logical ANDReturns first falsy value, or last truthy valueuser.isAdmin && renderAdminPanel()
||Logical ORReturns first truthy value, or last falsy valueconst name = input || "Anonymous"
??Nullish CoalescingReturns right-hand value ONLY if left-hand is null or undefinedconst port = process.env.PORT ?? 3000

Why ?? is Better than || for Defaults:

const count = 0;

// ❌ Bug: 0 is falsy, so || overrides it to 10
const pageSize1 = count || 10; // 10

// βœ… Correct: 0 is not null/undefined, so ?? preserves 0
const pageSize2 = count ?? 10; // 0

3. Optional Chaining (?.)

Optional chaining prevents TypeError: Cannot read properties of undefined when traversing deeply nested properties that might not exist:

const user = {
  profile: null,
};

// ❌ Crashes with TypeError: Cannot read properties of null
// const city = user.profile.address.city;

// βœ… Safe: returns undefined immediately without throwing
const city = user.profile?.address?.city;
console.log(city); // undefined

4. Conditional (Ternary) Operator

A concise inline replacement for if...else statements:

const status = isOnline ? "Active" : "Offline";

On this page