Tag: Switch Statement in JavaScript

1 articles found.

Switch Statement in JavaScript

Switch Statment in JavaScript javascript switch multiple case javascript switch case range javascript switch multiple case javascript switch return javascript switch case multiple expressions switch vs if else performance

Hello friends, I hope you all are doing great. In today’s tutorial, we will have a look at the detailed “ Switch Statement in JavaScript ”. It’s our 6th tutorial in JavaScript Series. As we have learned about the conditional statement and its types (if, if..else, If...else if) in the last article, a conditional statement is the most useful and common feature in the programming language. Switch is also a type of conditional statement and it will evaluate an expression against multiple possible cases and run one or more blocks of codes base on matching cases. The switched statement work almost as if...else if statement containing many blocks, and it does work so more efficiently than repeated if...else if statements. The switch statement is used to perform different action based on multiple cases. How does switch statement work? Syntax switch (expression) { case x: // case x code block (statement x) break; case y: // case y code block (statement y) break; default: // execute default code block (statement default) } As the name says break, When JavaScript reaches the break keyword, it breaks out of the switch block. It will only stop the execution of inside the block. It will continue until reaching the last break case. Eventually, reach the last case and the break keyword will end the switch block. If there is no case match then the default code block will run. There could be only one default keyword in the switch statement. The default keyword is optional, but it is ...