crea un switch in javascript

To create a switch statement in JavaScript, you can use the following syntax:

index.tsx
switch(expression) {
  case n:
    // code block
    break;
  case n:
    // code block
    break;
  default:
    // code block
}
130 chars
11 lines

The expression is the value that you want to test against. The case statements are the different values that you want to test for. If the expression matches a case statement, then the corresponding code block will run. You can have multiple case statements to test for different values.

The break statement is used to exit the switch statement. If you don't include a break, then the code will continue to run through the other case statements until it reaches the end or a break statement.

Here's an example of using a switch statement to display a message based on the day of the week:

index.tsx
var day = new Date().getDay();
switch(day) {
  case 0:
    console.log("Today is Sunday");
    break;
  case 1:
    console.log("Today is Monday");
    break;
  case 2:
    console.log("Today is Tuesday");
    break;
  case 3:
    console.log("Today is Wednesday");
    break;
  case 4:
    console.log("Today is Thursday");
    break;
  case 5:
    console.log("Today is Friday");
    break;
  case 6:
    console.log("Today is Saturday");
    break;
  default:
    console.log("Invalid day");
}
497 chars
27 lines

This code will output a message based on the day of the week. If the day is not a valid number (less than 0 or greater than 6), then it will display an "Invalid day" message.

gistlibby LogSnag