Coding/Learn/javascript/if else conditions
JavaScript Foundational Course
01.Introduction to JavaScript & V8 Engine
02.Console Output & Program Flow
03.Variables & Data Types (let, const, typeof)
04.Template Literals & String Interpolation
05.Type Coercion & Strict Equality (=== vs ==)
06.Arithmetic, Math Object & Modulo
07.Conditionals & If / Else Branching
08.Logical Operators & Nullish Coalescing (??)
09.Ternary Operator & Switch Statements
10.While & Do-While Loops
11.Standard For Loops & Iteration
12.Loop Control (Break & Continue)
13.Functions, Arrow Syntax & Return Values
14.Default Parameters, Rest & Spread Syntax
15.Arrays, Indexing & Mutating Methods
16.Array Iterators (map, filter, reduce)
17.Objects, Key-Value Pairs & Destructuring
18.Scope, Lexical Environment & Closures
19.Asynchronous JavaScript (Promises & Async/Await)
20.Capstone: Interactive E-Commerce Cart & Ledger Engine
Course Certificate0%

Complete all lessons to earn

LearnConditionals & If / Else Branching
7 / 20 0%
Lesson 07 of 20
5 mins read

Conditionals & If / Else Branching

PreviousNext Lesson
In Plain English

JavaScript uses if, else if, and else statements to execute code blocks based on boolean condition evaluations. Each branch is enclosed in curly braces creating its own block scope.

The Mental Model

"An if/else tree is like a railroad track switch. If the sensor detects a green signal, the train stays on track A; otherwise, it switches over to track B."

Deep Dive: How It Works

Condition Evaluation: Conditions are evaluated to truthy or falsy values.

Block Scope: Variables declared with const and let inside an if block cannot be accessed outside it.

Chaining Branches: Use else if to test multiple mutually exclusive conditions sequentially.

Syntax Blueprint

if (score >= 90) { ... } else if (score >= 80) { ... } else { ... }

Branches execute top-to-bottom and stop at the first condition that evaluates to truthy.

PointCore Rules to Remember

Point star
if Statement: Executes a block if its boolean expression evaluates to true.
Point star
else if Chain: Tests alternative conditions if previous checks were falsy.
Point star
else Fallback: Default catch-all block executed when no preceding conditions match.

Common Beginner Traps & How to Fix Them

Mistake:Using assignment (=) instead of equality (===) in condition.

Why it happens: if (x = 5) assigns 5 to x and evaluates to truthy, always entering the branch.

How to fix: Always use === or !== when comparing values inside if statements.

Pro Tip: Keep nested if statements shallow by using early return guard clauses.

Live Interactive Example

Hit Run Code to see it live
Branching Access Control
JavaScript ES2024
1
2
3
4
5
6
7
8
9
10
Output Console
Click "Run Code" to view the rendered output.
How it works: Since role is "admin" and verified is true, the first branch executes.

Your Turn: Micro Challenge

No pressure! Edit the starter code below and test your solution with instant feedback.

Micro Exercise

Grade evaluator conditional

  • PointGiven const score = 85;
  • PointIf score >= 90, log "Grade: A".
  • PointElse if score >= 80, log "Grade: B".
  • PointElse log "Grade: C".
1
2
3
4
Sandbox Output
Click "Run & Check" to test your solution.

Finished reading and practicing?

Mark this lesson as completed to update your course progress.

PreviousNext Lesson
Overview100 SheetLearnCertificates