Logic statements allow a program to choose between different execution paths based on conditions.
- A condition evaluates to true or false.
- A branch executes only when its condition is satisfied.
- Logic statements implement control flow.
if and if...else
Basic if
let temperature = 30;
if (temperature > 25){
console.log("It is warm outside.");
}
if...else
let isLoggedIn = false;
if(isLoggedIn){
console.log("Welcome back!");
}else{
console.log("Please log in first.");
}
else if Chain
let score=78;
if(score>=90){
console.log("Grade A");
}else if(score>=80){
console.log("Grade B");
}else if(score>=70){
console.log("Grade C");
}else{
console.log("Grade D");
}
Conditions are checked from top to bottom. The first true condition executes. Here score = 78 fails >=90 and >=80, but satisfies >=70, so "Grade C" is logged.
Truthy and Falsy
Falsy values: false, 0, -0, "" (empty string), null, undefined, NaN
let name="";
if(name){
console.log("Name is provided");
}else{
console.log("Name is missing");
}
Comparison and Logical Operators
| Operator | Meaning |
|---|---|
== | Equal (coercion) |
=== | Strict Equal |
!= | Not Equal |
!== | Strict Not Equal |
> | Greater Than |
< | Less Than |
>= | Greater Than or Equal |
<= | Less Than or Equal |
Logical Operators: && — AND · || — OR · ! — NOT
let age=20;
let hasID=true;
if(age>=18 && hasID){
console.log("Access granted");
}
let isAdmin=false;
let isModerator=true;
if(isAdmin || isModerator){
console.log("You can manage comments.");
}
let isComplete=false;
if(!isComplete){
console.log("Task is not complete yet.");
}
Conditional (Ternary) Operator
let age=17; let message=(age>=18)?"Adult":"Minor"; console.log(message);
The ternary operator is a concise alternative to simple if...else statements: condition ? valueIfTrue : valueIfFalse.
switch Statement
let day="Wednesday";
switch(day){
case "Monday":
console.log("Start of week");
break;
case "Wednesday":
console.log("Middle of week");
break;
default:
console.log("Another day");
}
break, execution falls through to the next case even if its condition doesn't match — always terminate each case with break unless fall-through is intentional.Choosing Between if and switch
Use if...else for ranges and inequalities. Use switch for matching a single variable against multiple exact values.
Complete Example — Movie Ticket Price Calculator
let age=16;
let isStudent=true;
let dayOfWeek="Wednesday";
let basePrice;
if(age<12){
basePrice=5;
}else if(age<18){
basePrice=8;
}else{
basePrice=10;
}
let discount=isStudent?2:0;
let finalPrice;
switch(dayOfWeek){
case "Wednesday":
finalPrice=basePrice-discount-1;
break;
default:
finalPrice=basePrice-discount;
}
console.log(finalPrice);
Trace: age=16 fails <12 but satisfies <18, so basePrice=8. isStudent is true, so discount=2. dayOfWeek is "Wednesday", so finalPrice = 8 - 2 - 1 = 5.
Chapter Summary
- if
- if...else
- else if
- Truthy & Falsy
- Comparison Operators
- Logical Operators
- Ternary Operator
- Switch Statement
- Fall-through
- Choosing between if and switch