J

JavaScript Handbook

Clean • Professional

JavaScript Conditional Statements — Top Interview Q&A

3 minute

JavaScript Conditional Statements — Interview Questions & Answers

Ques: What are conditional statements in JavaScript?

Ans: Conditional statements are used to perform different actions based on different conditions. They help your program make decisions — if something is true, do one thing; otherwise, do another.

JavaScript provides the following conditional statements:

  • if
  • if...else
  • if...else if...else
  • switch
  • ternary operator (?:)

Ques: How does the if statement work in JavaScript?

Ans: The if statement executes a block of code only if the condition evaluates to true.

Example:

let age = 18;
if (age >= 18) {
  console.log("You are an adult.");
}

Output:

You are an adult.

Ques: What is an if...else statement?

Ans: The if...else statement allows you to execute one block if true and another if false.

Example:

let isRaining = true;
if (isRaining) {
  console.log("Take an umbrella.");
} else {
  console.log("Enjoy the sunshine!");
}

Output:

Take an umbrella.

Ques: What is the if...else if...else ladder used for?

Ans: It is used when you have multiple conditions to check one after another.

Example:

let marks = 85;

if (marks >= 90) {
  console.log("Grade A");
} else if (marks >= 75) {
  console.log("Grade B");
} else if (marks >= 50) {
  console.log("Grade C");
} else {
  console.log("Fail");
}

Output:

Grade B

Ques: What is the ternary operator (?:) in JavaScript?

Ans: The ternary operator is a shorthand for if...else.

It takes three operands — condition, expression if true, expression if false.

Syntax:

condition ? expressionIfTrue : expressionIfFalse

Example:

let age = 20;
let result = (age >= 18) ? "Adult" : "Minor";
console.log(result);

Output:

Adult

Ques: What is a switch statement in JavaScript?

Ans: A switch statement is used to execute different blocks of code based on the value of a variable or expression.

It’s often used instead of multiple if...else conditions.

Example:

let day = 3;
switch (day) {
  case 1:
    console.log("Monday");
    break;
  case 2:
    console.log("Tuesday");
    break;
  case 3:
    console.log("Wednesday");
    break;
  default:
    console.log("Invalid day");
}

Output:

Wednesday

Ques: Why is the break keyword used in a switch statement?

Ans: The break statement stops further case evaluation.

Without break, JavaScript executes all subsequent cases (fall-through behavior).

Example:

let fruit = "apple";
switch (fruit) {
  case "apple":
    console.log("Red fruit");
    break;
  case "banana":
    console.log("Yellow fruit");
    break;
}

Ques: What is the default keyword in a switch statement?

Ans: default specifies the block of code that runs when no case matches the given value.

Example:

let color = "green";
switch (color) {
  case "red": console.log("Stop"); break;
  case "yellow": console.log("Wait"); break;
  default: console.log("Go");
}

Output:

Go

Ques: Can you use expressions in switch cases?

Ans: Yes, you can use expressions or function calls in case comparisons, but comparisons are strict (===).

Example:

let x = 10;
switch (true) {
  case (x > 0):
    console.log("Positive");
    break;
  case (x < 0):
    console.log("Negative");
    break;
  default:
    console.log("Zero");
}

Output:

Positive

Ques: What is fall-through in a switch statement?

Ans: Fall-through happens when you omit break, causing the execution to continue to the next case.

Example:

let num = 2;
switch (num) {
  case 1:
  case 2:
    console.log("One or Two");
    break;
  default:
    console.log("Other");
}

 

Article 0 of 0