Introductions
Conditional statements allow JavaScript to make decisions. They help a program decide what to do when a condition is true or false. In this chapter, you will practice if, if...else, else if, nested if, and ternary-style decision making through simple, real-world examples. The questions gradually move from basic conditions to slightly more practical problems. JavaScript Conditional Statements practice questions with solutions help to understand the concepts.
Question 1: Check Whether a Number is Positive
Problem
Create a variable named number. If the number is greater than 0, display "Positive Number".
Solution
let number = 15;
if (number > 0) {
console.log("Positive Number");
}
Output
Positive Number
Step-by-step Explanation
- The variable
numberstores15. - The
ifstatement checks whethernumber > 0. 15 > 0istrue.- Because the condition is true, JavaScript executes the code inside the
ifblock. "Positive Number"is displayed.
Question 2: Check Whether a Person is Eligible to Vote
Problem
A person must be at least 18 years old to vote. Write a JavaScript program that displays "Eligible to Vote" when the age is 18 or above.
Solution
let age = 20;
if (age >= 18) {
console.log("Eligible to Vote");
}
Output
Eligible to Vote
Step-by-step Explanation
agestores20.- The condition
age >= 18checks whether the age is at least 18. 20 >= 18istrue.- The
ifblock runs. - The program displays
"Eligible to Vote".
Question 3: Check Pass or Fail
Problem
A student passes an exam if their marks are 40 or more. Write a JavaScript program that displays "Pass" or "Fail".
Solution
let marks = 65;
if (marks >= 40) {
console.log("Pass");
} else {
console.log("Fail");
}
Output
Pass
Step-by-step Explanation
- The variable
markscontains65. - The condition checks whether
marks >= 40. 65 >= 40is true.- Therefore, the
ifblock executes and displays"Pass". - If the marks were below
40, theelseblock would display"Fail".
Question 4: Check Whether a Number is Even or Odd
Problem
Write a JavaScript program to check whether a number is even or odd.
Solution
let number = 12;
if (number % 2 === 0) {
console.log("Even");
} else {
console.log("Odd");
}
Output
Even
Step-by-step Explanation
numberstores12.%finds the remainder after division.12 % 2gives0.- The condition
number % 2 === 0is therefore true. - JavaScript displays
"Even". - If the remainder were not
0, the program would display"Odd".
Question 5: Check Positive, Negative, or Zero
Problem
Write a JavaScript program that checks whether a number is positive, negative, or zero.
Solution
let number = -8;
if (number > 0) {
console.log("Positive");
} else if (number < 0) {
console.log("Negative");
} else {
console.log("Zero");
}
Output
Negative
Step-by-step Explanation
- The variable
numbercontains-8. - First, JavaScript checks
number > 0. -8 > 0is false.- JavaScript then checks
number < 0. -8 < 0is true.- Therefore,
"Negative"is displayed. - The final
elsewould run only when the number is exactly0.
Question 6: Find the Greater of Two Numbers
Problem
Create two numbers and display which number is greater.
Solution
let number1 = 45;
let number2 = 70;
if (number1 > number2) {
console.log(number1 + " is greater");
} else if (number2 > number1) {
console.log(number2 + " is greater");
} else {
console.log("Both numbers are equal");
}
Output
70 is greater
Step-by-step Explanation
number1contains45.number2contains70.- The first condition checks whether
45is greater than70. - This condition is false.
- The second condition checks whether
70is greater than45. - This condition is true.
- JavaScript displays
"70 is greater".
The final else handles the situation where both numbers are equal.
Question 7: Give a Grade Based on Marks
Problem
Write a JavaScript program using if...else if...else to assign a grade:
- 90 or above →
A - 75 to 89 →
B - 60 to 74 →
C - 40 to 59 →
D - Below 40 →
F
Solution
let marks = 82;
if (marks >= 90) {
console.log("Grade A");
} else if (marks >= 75) {
console.log("Grade B");
} else if (marks >= 60) {
console.log("Grade C");
} else if (marks >= 40) {
console.log("Grade D");
} else {
console.log("Grade F");
}
Output
Grade B
Step-by-step Explanation
markscontains82.- JavaScript first checks whether marks are at least
90. 82 >= 90is false.- It then checks whether marks are at least
75. 82 >= 75is true.- JavaScript displays
"Grade B". - Once a matching condition is found, the remaining
else ifblocks are skipped.
Question 8: Check Driving Eligibility
Problem
A person can apply for a driving license if they are 18 or older. If they are younger than 18, display how many years they have to wait.
Solution
let age = 16;
if (age >= 18) {
console.log("You can apply for a driving license");
} else {
let yearsLeft = 18 - age;
console.log("Wait " + yearsLeft + " more years");
}
Output
Wait 2 more years
Step-by-step Explanation
- The variable
agecontains16. - JavaScript checks whether
age >= 18. - The condition is false.
- The
elseblock runs. 18 - 16gives2.- The result is stored in
yearsLeft. - JavaScript displays
"Wait 2 more years".
Question 9: Check Login Details
Problem
A website should allow login only when both the username and password are correct. Write a JavaScript program to check the login details.
Solution
let username = "admin";
let password = "12345";
if (username === "admin" && password === "12345") {
console.log("Login Successful");
} else {
console.log("Invalid Username or Password");
}
Output
Login Successful
Step-by-step Explanation
usernamestores"admin".passwordstores"12345".- The
&&operator requires both conditions to be true. - The username matches
"admin". - The password matches
"12345". - Therefore, the complete condition is true.
- JavaScript displays
"Login Successful".
Question 10: Check Whether a Number is Divisible by 5 and 10
Problem
Write a JavaScript program that checks whether a number is divisible by both 5 and 10.
Solution
let number = 50;
if (number % 5 === 0 && number % 10 === 0) {
console.log("The number is divisible by both 5 and 10");
} else {
console.log("The number is not divisible by both 5 and 10");
}
Output
The number is divisible by both 5 and 10
Step-by-step Explanation
numbercontains50.number % 5 === 0checks whether 50 is divisible by 5.50 % 5gives0, so the first condition is true.number % 10 === 0checks whether 50 is divisible by 10.50 % 10also gives0, so the second condition is true.- The
&&operator requires both conditions to be true. - Both conditions are true, so the first message is displayed.
Key Takeaways
- Conditional statements allow JavaScript to make decisions.
ifexecutes code when a condition is true.elseexecutes when theifcondition is false.else ifallows you to test multiple conditions.&&can be used when multiple conditions must be true.||can be used when at least one condition should be true.- Comparison operators such as
>,<,>=,<=,===, and!==are commonly used with conditions. %is useful for checking whether numbers are even, odd, or divisible by another number.- Conditions can be combined to solve practical problems.
- The order of
ifandelse ifconditions matters. - Only the first matching branch in an
if...else if...elsechain is executed.
FAQs
1. What is a conditional statement in JavaScript?
A conditional statement allows JavaScript to execute different code depending on whether a condition is true or false.
2. What is the use of an if statement?
An if statement executes a block of code only when its condition is true.
let age = 20;
if (age >= 18) {
console.log("Adult");
}
3. What is the purpose of else?
else provides an alternative block of code that runs when the if condition is false.
4. When should I use else if?
Use else if when you need to check multiple possible conditions.
let marks = 75;
if (marks >= 90) {
console.log("A");
} else if (marks >= 60) {
console.log("B");
} else {
console.log("C");
}
5. Can I use multiple else if statements?
Yes. You can use multiple else if blocks when a problem requires several conditions.
6. Can I use operators inside conditions?
Yes. Comparison and logical operators are commonly used inside conditions.
let age = 20;
if (age >= 18 && age <= 60) {
console.log("Eligible");
}
7. What happens when none of the if conditions are true?
If an else block exists, JavaScript executes it. If there is no else block, JavaScript simply skips the conditional statement.
Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.
