write a case statement in javascript

Here is an example of how to write a switch statement in JavaScript:

index.tsx
let value = 2;

switch (value) {
  case 1:
    console.log("Value is 1");
    break;
  case 2:
    console.log("Value is 2");
    break;
  case 3:
    console.log("Value is 3");
    break;
  default:
    console.log("Value is not 1, 2, or 3");
}
246 chars
16 lines

In this example, we are assigning the value 2 to the value variable. We then use a switch statement to compare value to different cases. Each case includes a block of code that will execute if value matches that case. In this example, the console.log() function will print "Value is 2" to the console.

If no cases match, the default block of code will execute. In this example, the console.log() function will print "Value is not 1, 2, or 3" to the console. The break statement is used after each case to prevent the code from falling through to the next case.

related categories

gistlibby LogSnag