30 JavaScript interview/practice questions focused on if, else if, and else statements, from basic to a bit tricky

🟢 Basic Level (1–10)

  1. What is an if statement in JavaScript?
  2. Write a program to check if a number is positive.
  3. How do you check if a number is even or odd using if?
  4. What is the syntax of if...else in JavaScript?
  5. Write a program to check if a person is eligible to vote.
  6. What happens if the condition inside if is false?
  7. Can we use multiple if statements without else?
  8. Write a program to check whether a number is greater than 100.
  9. How do comparison operators work in an if condition?
  10. What is the difference between == and === in an if statement?

🟡 Intermediate Level (11–20)

  1. Write a program to find the largest of two numbers using if.
  2. Write a program to find the largest of three numbers using if...else if.
  3. How do logical operators (&&, ||, !) work inside if conditions?
  4. Write a program to check whether a year is a leap year.
  5. What is nested if? Give an example.
  6. How does JavaScript treat non-boolean values in if conditions?
  7. What are truthy and falsy values? Name at least 5 falsy values.
  8. Write a program to check if a character is a vowel.
  9. What is the difference between if...else if and switch?
  10. Write a program to check whether a user is an adult and has a driving license.

🔴 Advanced / Tricky (21–30)

  1. What will be the output?
if ("0") {
  console.log("True");
} else {
  console.log("False");
}
  1. What will be the output?
if (0 == false) {
  console.log("Equal");
}
  1. What will be the output?
if (null) {
  console.log("Yes");
} else {
  console.log("No");
}
  1. What will be the output?
if (undefined == null) {
  console.log("Same");
}
  1. Can an if statement exist without curly braces {}? Explain.
  2. What is short-circuit evaluation in if conditions?
  3. How can you replace a simple if...else with a ternary operator?
  4. Write a program that checks multiple conditions in a single if statement.
  5. What is the scope of variables declared inside an if block?
  6. What are common mistakes developers make while using if statements in JavaScript?