Which Data Type Is Used in Switch Case?

//

Scott Campbell

In JavaScript, the switch case statement is a powerful tool for controlling program flow based on different values. It allows you to execute different blocks of code depending on the value of a specified expression. But have you ever wondered which data types can be used in a switch case?

Using Primitive Data Types

The switch case statement in JavaScript can work with several primitive data types. These include:

  • Number: You can use numbers as case expressions in a switch statement. For example:
  • 
      let day = 3;
      
      switch(day) {
        case 1:
          console.log("Monday");
          break;
        case 2:
          console.log("Tuesday");
          break;
        case 3:
          console.log("Wednesday");
          break;
        // .. other cases
        default:
          console.log("Invalid day");
      }
      
  • String: Strings are also valid case expressions. Here’s an example:
  • 
    let fruit = "apple";
    
    switch(fruit) {
      case "apple":
        console.log("It's an apple!");
        break;
      case "banana":
        console.log("It's a banana!");
        break;
     // . other cases
     default:
       console.log("Unknown fruit");
    }
    
  • Boolean: You can use boolean values as well. Here’s how:
  • 
    let isSunny = true;
    
    switch(isSunny) {
       case true:
         console.log("It's sunny today!");
         break;
       case false:
         console.log("It's not sunny today.");
         break;
       default:
         console.log("Unknown weather condition");
    }
    
  • Symbol: Symbols, introduced in ES6, can also be used as case expressions. Here’s an example:
  • 
    const COLOR_RED = Symbol("red");
    const COLOR_BLUE = Symbol("blue");
    
    let selectedColor = COLOR_RED;
    
    switch(selectedColor) {
      case COLOR_RED:
        console.log("The color is red!");
        break;
      case COLOR_BLUE:
        console.log("The color is blue!");
        break;
      default:
        console.log("Unknown color");
    }
    

Using Objects and Other Data Types

While primitive data types are commonly used in switch cases, it is important to note that objects and other data types like arrays are not directly supported. However, you can still achieve similar functionality by comparing object properties or array elements within the cases. Here’s an example:


let person = {
  name: 'John',
  age: 25
};

switch(true) {
  case (person.age < 18):
    console.log(person.name + " is a minor. ");
    break;
  case (person.age >= 18 && person.age < 60):
    console.name + " is an adult. 

");
    break;
  default:
    console.name + " is a senior citizen. ");
}

In this example, we compare the age property of the person object within each case to determine the appropriate output.

Conclusion

The switch case statement in JavaScript supports various primitive data types such as numbers, strings, booleans, and symbols. It provides a convenient way to execute different blocks of code based on the value of an expression. While objects and arrays cannot be directly used as case expressions, you can still achieve similar functionality by comparing properties or elements within the cases.

So next time you need to control program flow based on different values, remember the supported data types in switch cases and use them effectively!

Discord Server - Web Server - Private Server - DNS Server - Object-Oriented Programming - Scripting - Data Types - Data Structures

Privacy Policy