This series experience video tutorial => click to watch: Bilibili

A switch statement

The switch statement is also a conditional statement used to control a multi-branch process. The biggest difference from if is that if is usually conditional on a range, while switch is usually conditional on a point.

The basic grammar

switch(Matching expression) {caseEach branch expression: branch statement...break;
  caseEach branch expression: branch statement...break;
  default: Default branch statement...break;
}
Copy the code
  • The value of the matching expression is compared to each branch expression, and the corresponding branch code is executed.
  • breakStatements must be sufficient, or the following branch will execute without judgment
  • plusbreaktheswitchandifSimilarly, even if there are multiple matching branches, only one branch can be entered.
var fruit = 'apple'

switch (fruit) {
  case 'tomato':
    console.log('tomatoes! ');
    break;
  case 'apple':
    console.log('apple! ');
    break;
  default:
    console.log('default');
}

/ / apple!
Copy the code

Congruent matching

The matching rule uses congruence === instead of ==, which means that no type conversions occur when comparing.

var n = '1'
switch (n) {
  case 1: 
    console.log('the integer 1');
    break
  case 1.0: 
    console.log('the decimal 1.0');
    break
  case '1': 
    console.log('String 1');
}
Copy the code

Default position irrelevant

var n = 2
switch (n) {
  case 1: 
    console.log('the integer 1');
    break
  default:
    console.log('default');
    break; // Not at the bottom of 'default' remember to add 'break' as well
  case 1.0: 
    console.log('the decimal 1.0');
    break
}

/ / the default
Copy the code

Case statement merge

Multiple case statements with the same functionality can be combined, equivalent to or in an if statement

var day = 2
    
switch(day) {
  case 1:
  case 2:
  case 3:
  case 4:
  case 5:
    console.log('Working days! ');
    break;
  case 6:
  case 7: 
    console.log('Rest day! ');
    break;
  default : 
    console.log('Out of range! ');
}
Copy the code

Nesting of switch statements

var day = 1
var meal = 'supper'

switch(day) {
  case 1:
  case 2:
  case 3:
  case 4:
  case 5:
    console.log('Working days! ');
    switch (meal) {
      case 'breakfast': 
        console.log('Eggs and milk! ');
        break;
      case 'lunch': 
        console.log('fast food! ');
        break;
      case 'supper': 
        console.log('package! ');
        break;
    }
    break;
  case 6:
  case 7: 
    console.log('Rest day! ');
    switch (meal) {
      case 'breakfase': 
        console.log('Soy milk + churros! ');
        break;
      case 'lunch': 
        console.log('cooking! ');
        break;
      case 'supper': 
        console.log('Feast! ');
        break;
    }
    break;
  default : 
    console.log('Out of range! ');
}
Copy the code

Switch statements and object properties

Switch functionality can be implemented using object properties to simplify code

var meal = 'breakfast'

var obj = {
  breakfast: 'Eggs and milk! '.lunch: 'fast food! '.supper: 'package! '
}
console.log(obj[meal] ? obj[meal] : 'Not a meal! ');
Copy the code

practice

  • Print the days of the month based on the year and month
var year = 2000
var month = 2
switch (month) {
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
  console.log('31 days');
  break
case 4:
case 6:
case 9:
case 11:
  console.log('30 days');
  break
case 2: 
  // Non-century years are leap years that are divisible by 4 and not by 100.
  // Century years, divisible by 400, are leap years.
  // Implement with if
  if((year % 4= =0 && year % 100! =0) || year % 400= =0) {
    console.log('29 days');
  } else {
    console.log('28 days');
  }

  // Implement with switch
  switch ((year % 4= =0 && year % 100! =0) || year % 400= =0) {
    case true: 
      console.log('29 days');
      break;
    default: 
      console.log('28 days');
  }
  break
}
Copy the code

homework

  • Enter an 8-digit date number in the box prompted and determine the day of the year

Tip: You need to process the input 8-bit date string with % and/to get the year, month, and day. The number of days to be accumulated. You need to determine whether it is a leap year to determine the number of days in February.

// var dateStr = prompt(' Please enter 8-bit date ')
var dateStr = '20160316'
Copy the code