Here are 20 JavaScript programs with solutions covering:
✔ if
✔ if-else
✔ if-else if
✔ nested if
✔ one-line (ternary) conditions
🔹 1. Positive number (if)
let num = 5;if (num > 0) {
console.log("Positive");
}
🔹 2. Even or odd (if-else)
let num = 4;if (num % 2 === 0) {
console.log("Even");
} else {
console.log("Odd");
}
🔹 3. Voting eligibility (if-else)
let age = 16;if (age >= 18) {
console.log("Eligible");
} else {
console.log("Not eligible");
}
🔹 4. Largest of two numbers (if-else)
let a = 10, b = 20;if (a > b) {
console.log("a is greater");
} else {
console.log("b is greater");
}
🔹 5. Divisible by 3 (if)
let num = 9;if (num % 3 === 0) {
console.log("Divisible by 3");
}
🔹 6. Grade system (if-else if)
let marks = 85;if (marks >= 90) {
console.log("A");
} else if (marks >= 70) {
console.log("B");
} else if (marks >= 50) {
console.log("C");
} else {
console.log("Fail");
}
🔹 7. Largest of three numbers (if-else if)
let a = 5, b = 8, c = 3;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");
}
🔹 8. Leap year (if-else)
let year = 2024;if ((year % 4 === 0 && year % 100 !== 0) || year % 400 === 0) {
console.log("Leap year");
} else {
console.log("Not leap year");
}
🔹 9. Check number range (if)
let num = 25;if (num >= 10 && num <= 50) {
console.log("Within range");
}
🔹 10. Character type (if-else if)
let ch = 'A';if (ch >= 'A' && ch <= 'Z') {
console.log("Uppercase");
} else if (ch >= 'a' && ch <= 'z') {
console.log("Lowercase");
} else {
console.log("Other");
}
🔹 11. Nested if (positive & even)
let num = 8;if (num > 0) {
if (num % 2 === 0) {
console.log("Positive and Even");
}
}
🔹 12. Nested if (login system)
let username = "admin";
let password = "1234";if (username === "admin") {
if (password === "1234") {
console.log("Login successful");
} else {
console.log("Wrong password");
}
} else {
console.log("User not found");
}
🔹 13. Nested if (marks & distinction)
let marks = 75;if (marks >= 40) {
if (marks >= 75) {
console.log("Distinction");
} else {
console.log("Pass");
}
} else {
console.log("Fail");
}
🔹 14. One-line if (ternary)
let num = 10;console.log(num > 0 ? "Positive" : "Not positive");
🔹 15. One-line even/odd
let num = 7;console.log(num % 2 === 0 ? "Even" : "Odd");
🔹 16. One-line pass/fail
let marks = 35;console.log(marks >= 40 ? "Pass" : "Fail");
🔹 17. Multiple conditions (if)
let num = 15;if (num % 3 === 0 && num % 5 === 0) {
console.log("Divisible by 3 and 5");
}
🔹 18. Temperature check (if-else if)
let temp = 30;if (temp > 40) {
console.log("Hot");
} else if (temp > 20) {
console.log("Warm");
} else {
console.log("Cold");
}
🔹 19. Triangle validity (if)
let a = 3, b = 4, c = 5;if (a + b > c && a + c > b && b + c > a) {
console.log("Valid triangle");
}
🔹 20. Simple calculator (if-else if)
let a = 10, b = 5;
let op = "+";if (op === "+") {
console.log(a + b);
} else if (op === "-") {
console.log(a - b);
} else if (op === "*") {
console.log(a * b);
} else if (op === "/") {
console.log(a / b);
} else {
console.log("Invalid operator");
}
JavaScript if Condition Practice Programs (Without Solution)
Beginner Level
- Check whether a number is positive.
- Check whether a number is negative.
- Check whether a number is zero.
- Check whether a number is even or odd.
- Check whether a person is eligible to vote.
- Check whether a student passed or failed.
- Find the greater number between two numbers.
- Find the smaller number between two numbers.
- Check whether a character is a vowel or consonant.
- Check whether a year is a leap year.
Intermediate Level
- Check whether a number is divisible by 5.
- Check whether a number is divisible by both 3 and 7.
- Find the largest among three numbers.
- Find the smallest among three numbers.
- Check whether a character is uppercase or lowercase.
- Check whether a number is a 3-digit number.
- Check whether a person is a child, teenager, or adult.
- Check whether a triangle is valid based on angles.
- Check whether a temperature is hot, cold, or moderate.
- Calculate bonus based on salary conditions.
Advanced Beginner Level
- Check whether a number is divisible by 2 but not by 5.
- Check whether a student got distinction, first class, second class, or failed.
- Check whether a number is within a given range.
- Check whether three sides can form a triangle.
- Find the middle number among three numbers.
- Check whether a character is an alphabet, digit, or special character.
- Check whether a user entered the correct username and password.
- Check whether a number is a multiple of 10.
- Check whether a person is eligible for a driving license.
- Check whether an entered day is weekday or weekend.