Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

js switch case

switch(expression) {
  case x:
    // code block
    break;
  case y:
    // code block
    break;
  default:
    // code block
}
Comment

switch case in javascript

var color = prompt("enter color");

switch(color) {
    case "red":
      console.log("stop")
      break;
    case "green":
        console.log("go head")
      break;
      case "yellow":
        console.log("get ready")
      break;
    default:
        console.log("enter valid color")
  }
Comment

javascript switch

//javascript multiple case switch statement
var color = "yellow";
var darkOrLight="";
switch(color) {
    case "yellow":case "pink":case "orange":
        darkOrLight = "Light";
        break;
    case "blue":case "purple":case "brown":
        darkOrLight = "Dark";
        break;
    default:
        darkOrLight = "Unknown";
}

//darkOrLight="Light"
Comment

switch case javascript

int value = 0;
switch (value) {
  case 0:
    // do something
    break;
  case 1: 
    // do something else
    break;
   
  default :
  	// something if anything not match
}
Comment

js switch case

var color = "red";

switch (color) {
    case "blue":
        console.log("color is blue");
        break;
    case "white":
        console.log("color is white");
        break;
    case "black":
        console.log("color is black");
        break;
    case "red":
        console.log("color is red");
        break;

    default:
        console.log("color doesn't match ")
}

//Output: color is red;
Comment

switch case in javascript

function whatToDrink(time){
    var drink ;
          switch (time) {
            case "morning":
              drink = "Tea";
              break;
            case "evening":
              drink = "Shake";
              break;
            default:
              drink="Water";   
          }
  	return drink;
}
console.log(whatToDrink("morning")) //Tea
console.log(whatToDrink("evening")) //Shake
console.log(whatToDrink("night"))   //Water
console.log(whatToDrink("daytime")) //Water
Comment

switch js

switch (expr) {
  case 'Oranges':
    console.log('Oranges are $0.59 a pound.');
    break;
  case 'Apples':
    console.log('Apples are $0.32 a pound.');
    break;
  case 'Bananas':
    console.log('Bananas are $0.48 a pound.');
    break;
  case 'Cherries':
    console.log('Cherries are $3.00 a pound.');
    break;
  case 'Mangoes':
  case 'Papayas':
    console.log('Mangoes and papayas are $2.79 a pound.');
    break;
  default:
    console.log('Sorry, we are out of ' + expr + '.');
}

console.log("Is there anything else you'd like?");
Comment

javascript switch

let x = 3;

switch(x) {
  case 1:
    console.log("You entered 1");
    break;
  case 2:
    console.log("You entered 2");
    break;
  default:
    console.log("You entered: "+ x);
}
Comment

javascript switch

var color = "yellow";
var darkOrLight="";
switch(color) {
    case "yellow":case "pink":case "orange":
        darkOrLight = "Light";
        break;
    case "blue":case "purple":case "brown":
        darkOrLight = "Dark";
        break;
    default:
        darkOrLight = "Unknown";
}
Comment

switch javascript

var foo = 0;
switch (foo) {
  case -1:
    console.log('1 negativo');
    break;
  case 0: // foo es 0, por lo tanto se cumple la condición y se ejecutara el siguiente bloque
    console.log(0)
    // NOTA: el "break" olvidado debería estar aquí
  case 1: // No hay sentencia "break" en el 'case 0:', por lo tanto este caso también será ejecutado
    console.log(1);
    break; // Al encontrar un "break", no será ejecutado el 'case 2:'
  case 2:
    console.log(2);
    break;
  default:
    console.log('default');
}
Comment

JavaScript Switch Statement

 switch (new Date().getDay()) {
  case 0:
    day = "Sunday";
    break;
  case 1:
    day = "Monday";
    break;
  case 2:
     day = "Tuesday";
    break;
  case 3:
    day = "Wednesday";
    break;
  case 4:
    day = "Thursday";
    break;
  case 5:
    day = "Friday";
    break;
  case 6:
    day = "Saturday";
}     

console.log(day);
Comment

Switch Case Statement in JavaScript

switch(x){
    case value1: // if x === value1 
     ...      
        break;
    case value2: // if x === value2
        ...
        break;   
    default: // if x not match 
        ...
}
Comment

Javascript Switch Syntax

switch(condition){
    case 'value1' :
        //code
        [break;]
    case 'value2' :
        //code
        [break;]
    .......
    default :
        //code
        [break;]
}
Comment

javascript switch statement

switch (new Date().getDay()) {      // input is current day
case 6:                         // if (day == 6)
	text = "Saturday";          
	break;
case 0:                         // if (day == 0)
	text = "Sunday";
	break;
default:                        // else...
	text = "Whatever";
}
Comment

switch statement in javascript

switch ("oboe") {
  case "trumpet":
    console.log("I play the trumpet");
    break;
  case "flute":
    console.log("I play the flute");
    break;
  case "oboe":
    console.log("I play the oboe");
    break;
  default:
    console.log("I don't play an instrument. Sorry");
    break;
}
Comment

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"
Comment

switch javascript

switch(val) {
  case 1:
    // code
    break;
  case 2:
    // code
    break
}
Comment

JavaScript Checking in switch Statement

// program using switch statement
let a = 1;

switch (a) {
    case "1":
        a = 1;
        break;
    case 1:
        a = 'one';
        break;
    case 2:
        a = 'two';
        break;

    default:
        a = 'not found';
        break;
}
console.log(`The value is ${a}`);
Comment

JavaScript Switch Statement

switch(variable/expression) {
    case value1:  
        // body of case 1
        break;

    case value2:  
        // body of case 2
        break;

    case valueN:
        // body of case N
        break;

    default:
        // body of default
}
Comment

how to use switch case in javascript

switch (expression) {
  case value1:
    //Statements executed when the
    //result of expression matches value1
    [break;]
  case value2:
    //Statements executed when the
    //result of expression matches value2
    [break;]
  ...
  case valueN:
    //Statements executed when the
    //result of expression matches valueN
    [break;]
  [default:
    //Statements executed when none of
    //the values match the value of the expression
    [break;]]
}
Comment

switch JS

switch (expression) {
  case valeur1:
    // Instructions à exécuter lorsque le résultat
    // de l'expression correspond à valeur1
    instructions1;
    [break;]
  case valeur2:
    // Instructions à exécuter lorsque le résultat
    // de l'expression correspond à valeur2
    instructions 2;
    [break;]
  ...
  case valeurN:
    // Instructions à exécuter lorsque le résultat
    // de l'expression à valeurN
    instructionsN;
    [break;]
  [default:
    // Instructions à exécuter lorsqu'aucune des valeurs
    // ne correspond
    instructions_def;
    [break;]]
}
Comment

switch statement javascript

const dayNumber = new Date().getDay();
// Long-hand
let day;
switch (dayNumber) {
  case 0:
    day = "Sunday";
    break;
  case 1:
    day = "Monday";
    break;
  case 2:
    day = "Tuesday";
    break;
  case 3:
    day = "Wednesday";
    break;
  case 4:
    day = "Thursday";
    break;
  case 5:
    day = "Friday";
    break;
  case 6:
    day = "Saturday";
}
// Short-hand
const days = {
  0: "Sunday",
  1: "Monday",
  2: "Tuesday",
  3: "Wednesday",
  4: "Thursday",
  5: "Friday",
  6: "Saturday",
};
const day = days[dayNumber];
Comment

switch js

switch(expression) {
  case x:
    // code block
    break;
  default:
    // code block
}
Comment

PREVIOUS NEXT
Code Example
Javascript :: javascript array last element 
Javascript :: add table header dynamically in jquery 
Javascript :: Clone Array Using Spread Operator 
Javascript :: updatig state in react 
Javascript :: js key event 
Javascript :: js indexof string 
Javascript :: unknown provider angularjs 
Javascript :: javascript function return 
Javascript :: multilone input html 
Javascript :: How to get random no. without math.random() function 
Javascript :: send response from iframe to parent 
Javascript :: compare date value in javascript 
Javascript :: use the AJAX XMLHttpRequest object in Javascript to send json data to the server 
Javascript :: inline styling javascript 
Javascript :: react native use route undefined 
Javascript :: if else function react native 
Javascript :: Least common multiple from array 
Javascript :: Kendo grid export to Excel all pages 
Javascript :: es6 foreach dom element 
Javascript :: symbols javascript 
Javascript :: javascript console log 
Javascript :: express formidable 
Javascript :: hashnode 
Javascript :: react moment calendar times 
Javascript :: Simplest Promise Example 
Javascript :: change form value dynamically angular 
Javascript :: how to change Mime type of a file express 
Javascript :: javascript function hoisting 
Javascript :: reactjs 
Javascript :: frames[i] javascript 
ADD CONTENT
Topic
Content
Source link
Name
4+4 =