Tech Study

JavaScript switch statement

JavaScript Switch

JavaScript switch statement is used to perform different actions based on different conditions. It is similar to using a series of ‘if…else if…else’ statements, but is more efficient for testing multiple conditions.

Here is an example of using a ‘switch’ statement:

A JavaScript switch statement is a control flow statement that evaluates an expression and executes a block of code based on the value of that expression. 

The basic syntax of the switch statement is:

switch (expression) 

{

case value1:

 // statements to implement when expression matches value1

Break;

case value2:

// Code to implement if expression equals value2

Break;

case value_n:

// code to implement when expression matches value_n

break;

// You can add more cases here.

default:

// Code to implement if expression does not match any case

}
  • expression: 

An expression whose value is compared to a set of values (i.e: value1, value2, … value_n).

  • value1, value2, … value_n:

Values ​​to be compared with the expression.

  • Break:

Optional.The break statement is an optional statement that usually appears at the end of every block of code.

  • Default:

Optional. A block of code to be implemented if no value (eg value1, value2, .. value_n) matches the expression.

Example:

let fruit = “orange”;

let message;

switch (fruit) 

{

case "apple":

message = "Apples are rich in fiber and carotenoid..";

break;

case "banana":

message = "Banana is rich in potassium.";

break;

case "orange":

message = "Oranges are rich in vitamin C.";

break;

case "grape":

message = "Grapes contain the phytochemical resveratrol.";

break;

Default:

Message = Unsure what "" + fruit + " is.";

}

console.log(message);

Output:

Oranges are rich in vitamin C.

Benefitst of Switch Statement is JavaScript

There are several additional benefits of using a JavaScriot switch statement:

  • You can also use ‘switch’ statement as an expression by using ‘switch’ statement as the operand of a ternary operator.
  • ‘switch’ statements can be useful for simplifying complex conditional logic and making code easier to read and maintain. However, it’s important to use them judiciously and consider whether a series of ‘if…else’ statements might be more appropriate for a given situation.
  • ‘switch’ statements can make code more readable by clearly outlining the different conditions and corresponding actions. This can make it easier for other developers to understand the logic of the code.
  • ‘switch’ statements are generally more efficient than a series of ‘if…else’ statements. This is because the ‘switch’ statement only needs to evaluate the expression once, whereas an ‘if…else’ chain would need to evaluate the expression for each conditional statement.
  • ‘switch’ statements can simplify code maintenance by allowing developers to easily add or modify conditions and corresponding actions.
  • ‘switch’ statement can be used for better organization of code by breaking the code into different sections and each section will handle different cases.
  • ‘switch’ statement can be used as an expression by using ‘switch’ statement as the operand of a ternary operator. This can simplify code and make it more concise.

Conclusion:- ‘switch’ statement can be more efficient and readable than multiple ‘if…else’ statements. However, the best choice of control flow statement depends on the specific requirements of the code.

Java Final keyword

Introduction : java final keyword The final keyword present in Java programming language is generally used for restricting the user. …

Read more

C++ Memory Management: new and delete

C++ Memory Management We know that arrays store contiguous and the same type of memory blocks, so memory is allocated …

Read more