Track: JavaScript Basics
Lesson: JS-03 / JS-10
π― Lesson Goal
By the end of this lesson, you will be able to make decisions in JavaScript programs using conditional statements such as if, else, else if, and switch.
π Prerequisites
- JS-01: Variables and Data Types
- JS-02: Operators and Expressions
You should already understand:
- Variables
- Comparison operators (
>,<,===) - Logical operators (
&&,||)
π Why Do We Need Conditions?
In real life, decisions happen all the time:
- If it rains β take an umbrella
- If marks β₯ 40 β pass
- If user is logged in β show dashboard
JavaScript uses conditional statements to make such decisions.
πΉ The if Statement
The if statement executes code only if the condition is true.
Syntax
if (condition) {
// code to execute
}
Example
let age = 20;
if (age >= 18) {
console.log("You are eligible to vote");
}
β Output:
You are eligible to vote
πΉ The if...else Statement
Used when there are two possible outcomes.
Syntax
if (condition) {
// true block
} else {
// false block
}
Example
let marks = 35;
if (marks >= 40) {
console.log("Pass");
} else {
console.log("Fail");
}
β Output:
Fail
πΉ The else if Ladder
Used when there are multiple conditions.
Example
let score = 78;
if (score >= 90) {
console.log("Grade A");
} else if (score >= 75) {
console.log("Grade B");
} else if (score >= 60) {
console.log("Grade C");
} else {
console.log("Fail");
}
β Output:
Grade B
πΉ Using Logical Operators in Conditions
You can combine conditions using && (AND) and || (OR).
Example (AND)
let age = 22;
let hasID = true;
if (age >= 18 && hasID) {
console.log("Entry allowed");
}
Example (OR)
let isAdmin = false;
let isEditor = true;
if (isAdmin || isEditor) {
console.log("Access granted");
}
πΉ The switch Statement
Used when you want to compare one value against many options.
Syntax
switch (expression) {
case value1:
// code
break;
case value2:
// code
break;
default:
// code
}
Example
let day = 3;
switch (day) {
case 1:
console.log("Monday");
break;
case 2:
console.log("Tuesday");
break;
case 3:
console.log("Wednesday");
break;
default:
console.log("Invalid day");
}
β Output:
Wednesday
π΄ Important:
Always use break to stop execution.
πΉ Conditional (Ternary) Operator
A short form of if...else.
Syntax
condition ? value_if_true : value_if_false;
Example
let result = (age >= 18) ? "Adult" : "Minor";
console.log(result);
β οΈ Common Mistakes
- Using
=instead of=== - Forgetting curly braces
{ } - Missing
breakinswitch - Writing overly complex conditions in one line
π§ Practice Tasks
- Check whether a number is positive or negative
- Write a program to check pass/fail using marks
- Assign grades using
else if - Use
switchto display weekday names - Convert an
if...elseinto a ternary operator
π§ͺ Mini Coding Challenge
let temperature = 32;
Write a condition to display:
- βColdβ if temp < 20
- βNormalβ if 20β30
- βHotβ if > 30
π Summary
ifexecutes code when condition is trueif...elsehandles two outcomeselse ifhandles multiple conditionsswitchis useful for fixed values- Ternary operator is a shortcut for simple decisions
β‘οΈ Next Lesson
JavaScript Loops (JS-04)
π Quiz & Badge
Quiz and badges will be enabled soon.