JavaScript Switch Statements


What is a switch statement in JavaScript?

A switch statement evaluates an expression and executes code blocks based on matching cases. It is often used as an alternative to multiple if statements when checking the same variable.


What is the basic syntax of a switch statement?

The basic syntax of a switch statement is as follows:


switch (expression) {
  case value1:
    // Code to execute if expression equals value1
    break;
  case value2:
    // Code to execute if expression equals value2
    break;
  default:
    // Code to execute if no case matches
}

Can you provide an example of a switch statement?


let fruit = 'apple';

switch (fruit) {
  case 'banana':
    console.log('Banana is yellow.');
    break;
  case 'apple':
    console.log('Apple is red.'); // This will be executed
    break;
  default:
    console.log('Unknown fruit.');
}

What is the significance of the break statement in a switch case?

The break statement is used to exit the switch block after a case has been executed. Without it, the program will continue executing the subsequent cases (fall-through behavior).


let fruit = 'banana';

switch (fruit) {
  case 'banana':
    console.log('Banana is yellow.');
    // No break, so the next case will also execute
  case 'apple':
    console.log('Apple is red.'); // This will be executed
    break;
  default:
    console.log('Unknown fruit.');
}

What happens if you omit the break statement in a switch case?

If you omit the break statement, the code will "fall through" to the next case, executing the code in the subsequent case(s) until a break is encountered or the switch statement ends.


let day = 2;

switch (day) {
  case 1:
    console.log('Monday');
    break;
  case 2:
    console.log('Tuesday'); // This will be executed
  case 3:
    console.log('Wednesday'); // This will also be executed
    break;
  default:
    console.log('Another day');
}
// Output: Tuesday
//         Wednesday

What is the purpose of the default case in a switch statement?

The default case is executed if none of the specified cases match the value of the expression. It acts like the else statement in an if-else chain.


let animal = 'dog';

switch (animal) {
  case 'cat':
    console.log('This is a cat.');
    break;
  case 'bird':
    console.log('This is a bird.');
    break;
  default:
    console.log('Unknown animal.'); // This will be executed
}

Can you use expressions in the case statements?

Yes, you can use expressions in case statements, as long as they evaluate to a value that can be compared with the switch expression. However, keep in mind that case statements must be unique.


let score = 85;

switch (true) {
  case score >= 90:
    console.log('Grade: A');
    break;
  case score >= 80:
    console.log('Grade: B'); // This will be executed
    break;
  case score >= 70:
    console.log('Grade: C');
    break;
  default:
    console.log('Grade: F');
}

What types of values can you use in switch statements?

In switch statements, you can use values of any type that can be compared using the strict equality operator (===). This includes numbers, strings, and other primitive types.


let color = 'blue';

switch (color) {
  case 'red':
    console.log('Red color');
    break;
  case 'blue':
    console.log('Blue color'); // This will be executed
    break;
}

How do you handle multiple cases that execute the same code block?

You can handle multiple cases that execute the same code block by listing them sequentially without break statements in between.


let grade = 'B';

switch (grade) {
  case 'A':
  case 'B':
    console.log('Excellent'); // This will be executed for both A and B
    break;
  case 'C':
    console.log('Good');
    break;
  default:
    console.log('Needs Improvement');
}

What are the limitations of using switch statements?

Limitations of using switch statements include:

  • They only perform strict equality checks, meaning type coercion does not occur.
  • They can lead to fall-through behavior if not managed with break statements, potentially causing unintended code execution.
  • They can become unwieldy with a large number of cases, making the code harder to read and maintain.
Ads