Is the user logged in? → true/falseIs the password correct? → true/falseIs the cart total above 500? → true/falseDid the player lose all lives? → true/falseComparing Values and Combining Conditions in JavaScript
Bottom-Line Answer: JavaScript (JS) is a high-level, single-threaded, just-in-time (JIT) compiled programming language with first-class functions that conforms to the ECMAScript specification. It powers dynamic client-side web interactivity alongside HTML and CSS, and executes backend server runtimes via Node.js, Deno, and Bun.
What is Comparing Values and Combining Condition? Core Concept & Direct Summary
How do logical AND (&&), OR (||), and Nullish Coalescing (??) operators work?
very time, is the same two skills:comparing valuesandcombining conditions. Is the password===the stored one? Is the cart total>=500ANDis the user logged in?
we touredeveryoperator category. In this lesson, we slow down and master the two categories that power 90% of real-world logic:comparisonandlogical operators— plus the two secret concepts that make them truly work:truthy/falsy valuesandshort-circuit evaluation. Let's make your code smart. 🧠
How Does Comparing Values and Combining Condition Work? In-Depth Mechanics & Architecture

Why Comparison Is the Heart of Every Program
Strip away the fancy frameworks, and every program is just a long series of questions:
Every question is acomparison, and every comparison produces aboolean—trueorfalse. That boolean then drives a decision (which we'll fully explore in the next lesson onif/else).
So let's make sure your questions arecorrect— because asking the wrong question is how bugs are born. 🐛
Part 1: Comparing Values — The Full Toolkit
Equality: The === Family
You met these in Lesson 6, but here's the quick reference:
The rule you should tattoo on your brain:always use===/!==. The loose versions (==,!=) secretly convert types, and their surprises are legendary:
With===, all of these are honestlyfalse. No conversion, no surprises.
Relational: Greater, Less, and Friends
💡 Interview gem:
>=includes equality. The most common off-by-one bug in history is using>where>=was needed — "must be 18or older" isage >= 18, notage > 18(which rejects 18-year-olds!).
⚠️ Comparing Strings: Dictionary Rules
Strings compareletter by letter, like a dictionary — based on character codes:
That last line causes real bugs: comparing numeric strings compares themas text. Convert first:Number("10") > Number("9")→true. ✅
⚠️ The Special Case: NaN Can Never Equal Anything
RememberNaNfromLesson 5? It's the only value in JavaScript that isnot equal to itself:
To check for NaN, never use===— use:
Part 2: Truthy and Falsy — The Hidden Rule 🎭
Here's something most tutorials mention too late:JavaScript conditions don't require actual booleans.Every single value in JavaScript is secretly eithertruthy(acts liketrue) orfalsy(acts likefalse) when used in a condition.
The Complete Falsy List (Only 8 Exist!)
Everything else is truthy.Memorize the falsy list — it's tiny, and everything NOT on it is truthy:
The!!Trick — Revealing Any Value's Truthiness
Double-NOT converts any value to its true boolean nature:
Why This Matters in Real Code
Truthy/falsy checks let you write beautifully clean conditions:
No=== ""needed — the string itself answers the question. This pattern appearseverywherein professional code.
Part 3: Combining Conditions — && || ! in Real Life
One question is rarely enough. Real decisions combine several:
&&(AND) — The Bouncer 🕶️
Everyone must pass:
Onefalseruins everything:
||(OR) — The Flexible Friend 🤝
Any single pass is enough:
!(NOT) — The Reverser 🔄
Combining Multiple Operators — Real-World Example
An e-commerce free-shipping rule:"Order must be ₹500+, AND (the user is a member OR has a coupon), AND the address is not remote."
Notice the parentheses—(isMember || hasCoupon)groups the OR so it's evaluated as one unit. When mixing&&and||, always use parentheses for clarity. (&&runs before||by default — but explicit grouping makes your intent obvious to every reader, including future you.)
Part 4: Short-Circuit Evaluation ⚡ — The Secret Behavior
Here's what almost no beginner tutorial explains:&&and||don't just return true/false — they return actual values, and they stop checking as soon as the answer is decided. This is calledshort-circuit evaluation.
||Returns the First Truthy Value
This is how professionals write default values:
You'll see this pattern in every real codebase. It's the older sibling of??(which you met in Lesson 6 —??only falls back onnull/undefined, while||falls back onanyfalsy value including""and0).
&&Returns the First Falsy Value (or the Last One Standing)
Real-world use — conditional execution:
Why "Short-Circuit"? Efficiency + Safety
JavaScript stops evaluating the moment the result is certain — which protects you from errors:
This is exactly how?.(optional chaining, Lesson 6) works under the hood.
Common Beginner Mistakes (And Fixes)
⚠️ The chained-comparison trapdeserves emphasis:
18 <= age <= 65looks natural but breaks. JavaScript evaluates18 <= agefirst (→true/false), then comparesthat booleanto65. Always writeage >= 18 && age <= 65.
Quick Self-Check: Lesson 7 Quiz 🧠
- List anyfourfalsy values in JavaScript.
- What's the output?
console.log("" || "Guest"); - What's the output?
console.log(true && "Hello"); - Fix this buggy code:
if (age = 21) { console.log("Adult"); } - Why does
console.log("10" < "9");returntrue?
(Answers: 1. Any four of: false, 0, -0, 0n, "", null, undefined, NaN · 2. "Guest" — empty string is falsy, so || moves on · 3. "Hello" — && with all truthy returns the last value · 4.if (age === 21)— single = assigns instead of comparing · 5. Strings compare character-by-character; "1" has a lower character code than "9" — convert to numbers first)
JavaScript Decision Matrix & Syntax Comparison
Understanding the mechanical differences between variable declarations and execution environments is critical for writing performant, bug-free modern JavaScript.
var vs let vs const| Feature / Property | var (Legacy ES5) | let (Modern ES6+) | const (Modern ES6+) |
|---|---|---|---|
| Scope | Function Scope | Block Scope ({}) | Block Scope ({}) |
| Hoisting Behavior | Hoisted (initialized as undefined) | Hoisted (Temporal Dead Zone - ReferenceError) | Hoisted (Temporal Dead Zone - ReferenceError) |
| Reassignable | ✓ Yes | ✓ Yes | ✗ No (Immutable binding) |
| Redeclarable in Same Scope | ✓ Yes (Error-prone) | ✗ No (SyntaxError) | ✗ No (SyntaxError) |
| Window Object Binding | Creates property on window | Does not attach to window | Does not attach to window |
| Dimension | Client-Side (Web Browser) | Server-Side (Node.js / Deno) |
|---|---|---|
| Global Object | window, document, navigator | global, process, globalThis |
| DOM & UI Manipulation | ✓ Full access (document.querySelector) | ✗ No DOM (Headless / Non-UI) |
| File System & OS Access | ✗ Blocked (Sandboxed security) | ✓ Full access (fs, os, child_process) |
| Primary Focus | Interactive UI, animations, client state | REST APIs, microservices, databases, I/O streaming |
Runnable JavaScript Code Example & Practical Implementation
let count = 0;
let val1 = count || 10;
// 10 (because 0 is falsy)
let val2 = count ?? 10;
// 0 (because 0 is not nullish)
console.log(val1, val2);Frequently Asked Questions & Technical Interview Insights
Technical Interview & Viva Questions
Frequently asked questions for frontend & full-stack rolesQ: What are falsy values in JavaScript?
Related Tools, Tutorials & Authoritative References
Recommended Interactive Tools
- Online JavaScript Editor & IDE — Browser-based JavaScript workspace with live console and DOM inspector.
- C Programming Tutorial — Master memory allocation, pointers, and systems programming fundamentals.
- C++ Programming Tutorial — Explore Object-Oriented Programming, templates, and high-performance STL.
- Online Code Editors & Compilers — Complete development tool suite for 20+ programming languages.
Authoritative Standards & Citations
- ECMA-262 ECMAScript Language Specification — The official international JavaScript language specification maintained by TC39.
- MDN Web Docs (Mozilla Developer Network) — Authoritative documentation and API guide for JavaScript and Web APIs.
- Google V8 JavaScript Engine — Open-source high-performance JavaScript and WebAssembly engine documentation.
- Node.js Official Documentation — Server-side JavaScript runtime API specification and reference.