menu

JavaScript/Typescript - 16 Topics

DEEP DIVE INTO

JavaScript/Typescript

Topic:switch-case statement

menu

In JavaScript, switch statements are used to make decisions based on the value of an expression. It provides a way to compare a single value to multiple possible statement values ​​and generate code segments based on the match. Switch statements are especially useful when you have a single value to compare multiple possibilities.

Here is a detailed explanation of the switch issue.

Basic Syntax:

javascriptswitch (expression) {
  case value1:
    // Executable  Code to be executed if expression === value1
    break;
  case value2:
    // Executable  Code to be executed if expression === value2
    break;
  // Additional cases...
  default:
    // Executable  Code to be executed if no case matches
}
  • expression: The value you want to compare different cases with.

  • case value1: A case label with a specific value to which you compare the expression.

  • // Executable code: The code to be executed if the expression matches the value of the case label.

  • break: The break statement is used to exit the switch block after the match case code is executed. It is important to prevent the "falling off" of subsequent cases.

For Example:

javascriptconst day = 'Wednesday';

switch (day) {
  case 'Monday':
    console.log('Start of the workweek');
    break;
  case 'Wednesday':
    console.log('Middle of the workweek');
    break;
  case 'Friday':
    console.log('End of the workweek');
    break;
  default:
    console.log('Weekend or unknown day');
}

In this example, the code analyzes the value of the "day" variable and generates blocks of code based on its value.

Multiple Cases:

You can add multiple statements sharing the same code by omitting the break statement between them.

javascriptconst fruit = 'apple';

switch (fruit) {
  case 'apple':
  case 'banana':
  case 'cherry':
    console.log('This is a fruit from the red group.');
    break;
  case 'kiwi':
  case 'grape':
  case 'pear':
    console.log('This is a fruit from the green group.');
    break;
  default:
    console.log('Unknown fruit.');
}

No break Statement:

If you omit the break statement, the code executes subsequent cases until it encounters a break or reaches the end of a switch block. This is called "falling through".

For Example:

javascriptconst num = 2;

switch (num) {
  case 1:
    console.log('One');
  case 2:
    console.log('Two');
  case 3:
    console.log('Three');
  default:
    console.log('Other number');
}

Since there is no break statement in this example, if num is 2, the code will be 'Two', 'Three', and 'Other number'.

default Case:

The default case is optional and is executed if none of the other cases match the value of the expression. It is similar to the "else" clause in a switch statement.

Use Cases:

  • Switch statements are useful when you have a single value to compare against multiple possible values, which allows you to select multiple if-else statements.

  • This can improve code readability and maintainability, especially when you have explicit specifications.

In summary, switch statements in JavaScript enable you to effectively evaluate an expression against many possible criteria and create blocks of code based on conventions are a valuable decision-making tool in your code of the system, especially when you have known possible values ​​for comparison.

1280 x 720 px