Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

case switch javascript

switch(expression) {

    case x:
       // code of block to be executed when this case matches
       break; // if you decide to run a return statement in a specific case, then don't write break
    case y:
       // code of block to be executed when this case matches
       return "xyz"
       // In this case no "break" will be written as we have written a return statement
    default: "xyz" // If no case matches then the code written in the block of "default" will be executed

}

// For example

let x = 5;

switch(x) {
  case 1: 
    console.log(`x value is ${x}`)   
  case 2: 
    console.log(`x value is ${x}`)   
  case 3: 
    console.log(`x value is ${x}`)   
  case 4: 
    console.log(`x value is ${x}`)   
  case 5: 
    console.log(`x value is ${x}`)   
}
 
// Output "x value is 5"
Source by www.cluemediator.com #
 
PREVIOUS NEXT
Tagged: #case #switch #javascript
ADD COMMENT
Topic
Name
4+6 =