Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

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

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

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

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

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

PREVIOUS NEXT
Code Example
Javascript :: HSET in redis 
Javascript :: Backbone Router 
Javascript :: Split string on the first white space occurrence 
Javascript :: express sendfile root path 
Javascript :: jest Cross origin http://localhost forbidden 
Javascript :: javascript string reverse 
Javascript :: qr code terminal npm 
Javascript :: is an Angular component, then verify that it is part of this module. 
Javascript :: javascript closure interview questions 
Javascript :: filter array js 
Javascript :: reverse array in js 
Javascript :: update in sequelize 
Javascript :: send sms with twilio 
Javascript :: math module js 
Javascript :: javascript example 
Javascript :: how to add eventlister to multiple variable 
Javascript :: create multiple buttons in javascript 
Javascript :: js alert with multiple buttons 
Javascript :: regex checker 
Javascript :: javascript debugger 
Javascript :: aframe react 
Javascript :: javascript substration between times 
Javascript :: currency conversion to locale string js 
Javascript :: create owl component react js 
Javascript :: Install PHP debugbar 
Javascript :: js setinterval run immediately 
Javascript :: jquery document ready deprecated 
Javascript :: angular component 
Javascript :: jsx react 
Javascript :: local forage 
ADD CONTENT
Topic
Content
Source link
Name
3+6 =