Using Switch Statement to coding in Javascript/JS

This JavaScript tutorial explains how to use the change assertion with syntax and examples.

Description

In JavaScript, the switch announcement is used to execute code based on the value of an expression.

Syntax

The syntax for the swap assertion in JavaScript is:

switch (expr) {
   case value1:
     // statements to execute when expr matches value1
     break;

   case value2:
     // statements to execute when expr matches value2
     break;

   case value_n:
     // statements to execute when expr matches value_n
     break;

   default:
     // statements to execute when expr does not match any of the values
}

Parameters or Arguments

expr An expression whose price is compared to a set of values (ie: value1, value2, … value_n). value1, value2, … value_n These are the values that are in contrast to expr. These values are preceded through the case key-word and are evaluated in the order listed. Once a price fits expr, the swap statement will execute the corresponding statements. It is essential to notice that the swap assertion performs the comparison the usage of the === operator which potential that expr need to be equal to the price as well as the equal records kind in order to be regarded a match. break Optional. The destroy announcement is an non-obligatory assertion that typically seems at the end of every code block. It is used to terminate the swap statement. This capability that no in addition values in the change assertion will be in contrast to expr. If you do not consist of a break statement, JavaScript will continue evaluating the subsequent values in the change statement and you may also get sudden results. default Optional. It is the block of code that will be carried out if none of the values (ie: value1, value2, .. value_n) suit expr. If the default label is discovered at the stop of your switch statement, it is now not critical to include a destroy statement.

Note

The switch assertion will execute the code block for the first cost that suits expr. The ruin assertion ensures that the switch statement is terminated so that no different values are compared to expr. If no value fits expr, the code block inside the default area of the switch announcement will be executed, if present.

Example

The following is example the usage of the switch statement in JavaScript:

// Set the TechOnTheNet technology to JavaScript
var totn_technology = 'JavaScript';

switch (totn_technology) {
   case 'SQL':
     console.log('TechOnTheNet SQL');
     break;

   case 'JavaScript':
     console.log('TechOnTheNet JavaScript');
     break;

   default:
     console.log('Other TechOnTheNet technologies');
}

In this switch announcement example, the code will execute exceptional statements relying on the price of the totn_technology variable. Since the totn_technology variable has been set to the string ‘JavaScript’, the statements associated with the case ‘JavaScript’: label will be executed.

In this example, the following will be output to the net browser console log:

TechOnTheNet JavaScript

Sharing Code Blocks

In JavaScript, you can additionally share code blocks between values.

For example:

// Set the TechOnTheNet technology to JavaScript
var totn_technology = 'JavaScript';

switch (totn_technology) {
   case 'SQL':
     console.log('TechOnTheNet SQL');
     break;

   case 'JavaScript':
   case 'HTML':
     console.log('TechOnTheNet Web Development');
     break;

   default:
     console.log('Other TechOnTheNet technologies');
}

In this example, the values ‘JavaScript’ and ‘HTML’ share a code block. Since the totn_technology variable has been set to the string ‘JavaScript’, the switch announcement will execute this shared code block and output the following to the net browser console log:

TechOnTheNet Web Development