JavaScript Conditional Statements Practice Questions with Solutions

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

  1. The variable number stores 15.
  2. The if statement checks whether number > 0.
  3. 15 > 0 is true.
  4. Because the condition is true, JavaScript executes the code inside the if block.
  5. "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

  1. age stores 20.
  2. The condition age >= 18 checks whether the age is at least 18.
  3. 20 >= 18 is true.
  4. The if block runs.
  5. 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

  1. The variable marks contains 65.
  2. The condition checks whether marks >= 40.
  3. 65 >= 40 is true.
  4. Therefore, the if block executes and displays "Pass".
  5. If the marks were below 40, the else block 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

  1. number stores 12.
  2. % finds the remainder after division.
  3. 12 % 2 gives 0.
  4. The condition number % 2 === 0 is therefore true.
  5. JavaScript displays "Even".
  6. 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

  1. The variable number contains -8.
  2. First, JavaScript checks number > 0.
  3. -8 > 0 is false.
  4. JavaScript then checks number < 0.
  5. -8 < 0 is true.
  6. Therefore, "Negative" is displayed.
  7. The final else would run only when the number is exactly 0.

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

  1. number1 contains 45.
  2. number2 contains 70.
  3. The first condition checks whether 45 is greater than 70.
  4. This condition is false.
  5. The second condition checks whether 70 is greater than 45.
  6. This condition is true.
  7. 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

  1. marks contains 82.
  2. JavaScript first checks whether marks are at least 90.
  3. 82 >= 90 is false.
  4. It then checks whether marks are at least 75.
  5. 82 >= 75 is true.
  6. JavaScript displays "Grade B".
  7. Once a matching condition is found, the remaining else if blocks 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

  1. The variable age contains 16.
  2. JavaScript checks whether age >= 18.
  3. The condition is false.
  4. The else block runs.
  5. 18 - 16 gives 2.
  6. The result is stored in yearsLeft.
  7. 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

  1. username stores "admin".
  2. password stores "12345".
  3. The && operator requires both conditions to be true.
  4. The username matches "admin".
  5. The password matches "12345".
  6. Therefore, the complete condition is true.
  7. 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

  1. number contains 50.
  2. number % 5 === 0 checks whether 50 is divisible by 5.
  3. 50 % 5 gives 0, so the first condition is true.
  4. number % 10 === 0 checks whether 50 is divisible by 10.
  5. 50 % 10 also gives 0, so the second condition is true.
  6. The && operator requires both conditions to be true.
  7. Both conditions are true, so the first message is displayed.

Key Takeaways

  • Conditional statements allow JavaScript to make decisions.
  • if executes code when a condition is true.
  • else executes when the if condition is false.
  • else if allows 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 if and else if conditions matters.
  • Only the first matching branch in an if...else if...else chain 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.

Scroll to Top