✅ 1. What is an if condition?
An if statement is used to execute code only when a condition is true.
📌 Syntax:
if (condition) {
// code runs if condition is true
}
✅ Example:
let age = 18;if (age >= 18) {
console.log("You are eligible to vote");
}
✅ 2. if...else Statement
Used when you want to handle both true and false cases.
📌 Syntax:
if (condition) {
// true block
} else {
// false block
}
✅ Example:
let number = 5;if (number % 2 === 0) {
console.log("Even number");
} else {
console.log("Odd number");
}
✅ 3. if...else if...else
Used when you have multiple conditions.
📌 Syntax:
if (condition1) {
// block 1
} else if (condition2) {
// block 2
} else {
// default block
}
✅ Example:
let marks = 75;if (marks >= 90) {
console.log("Grade A");
} else if (marks >= 70) {
console.log("Grade B");
} else {
console.log("Grade C");
}
✅ 4. Nested if
An if inside another if.
✅ Example:
let age = 20;
let hasLicense = true;if (age >= 18) {
if (hasLicense) {
console.log("You can drive");
} else {
console.log("Get a license first");
}
}
✅ 5. Ternary Operator (Short if-else)
A short form of if...else.
📌 Syntax:
condition ? value1 : value2;
✅ Example:
let age = 16;let result = (age >= 18) ? "Adult" : "Minor";
console.log(result);
🔷 Important Concepts
🔹 Comparison Operators
| Operator | Meaning |
|---|---|
== | Equal (loose) |
=== | Equal (strict) |
!= | Not equal |
!== | Strict not equal |
> < >= <= | Comparison |
👉 Always prefer === (strict equality)
🔹 Logical Operators
| Operator | Meaning |
|---|---|
&& | AND |
! | NOT |
✅ Example:
let age = 20;
let hasID = true;if (age >= 18 && hasID) {
console.log("Allowed entry");
}
🔷 Practice Programs (Very Important 🚀)
🟢 Beginner Level
1. Check Positive or Negative
let num = -5;if (num >= 0) {
console.log("Positive");
} else {
console.log("Negative");
}
2. Find Largest of Two Numbers
let a = 10, b = 20;if (a > b) {
console.log("A is larger");
} else {
console.log("B is larger");
}
3. Check Divisible by 5
let num = 25;if (num % 5 === 0) {
console.log("Divisible by 5");
} else {
console.log("Not divisible");
}
🟡 Intermediate Level
4. Find Largest of Three Numbers
let a = 10, b = 25, c = 15;if (a > b && a > c) {
console.log("A is largest");
} else if (b > c) {
console.log("B is largest");
} else {
console.log("C is largest");
}
5. Check Leap Year
let year = 2024;if ((year % 4 === 0 && year % 100 !== 0) || year % 400 === 0) {
console.log("Leap Year");
} else {
console.log("Not a Leap Year");
}
6. Grade Calculator
let marks = 85;if (marks >= 90) {
console.log("A");
} else if (marks >= 75) {
console.log("B");
} else if (marks >= 50) {
console.log("C");
} else {
console.log("Fail");
}
🔴 Advanced Level
7. Check Login Access
let username = "admin";
let password = "1234";if (username === "admin" && password === "1234") {
console.log("Login successful");
} else {
console.log("Invalid credentials");
}
8. Check Even/Odd using Ternary
let num = 7;let result = (num % 2 === 0) ? "Even" : "Odd";
console.log(result);
9. Traffic Light System
let color = "red";if (color === "red") {
console.log("Stop");
} else if (color === "yellow") {
console.log("Get Ready");
} else if (color === "green") {
console.log("Go");
} else {
console.log("Invalid color");
}
🔷 Pro Tips 💡
- Use
===instead of== - Keep conditions simple and readable
- Avoid deeply nested
if→ useelse ifinstead - Use ternary only for short conditions
🔷 Practice Questions (Try Yourself )
- Check if a number is divisible by both 3 and 7
- Check whether a character is vowel or consonant
- Find smallest of three numbers
- Check if a number is palindrome
- Create simple ATM withdrawal condition