JAVASCRIPT BASICS
- Introduction to JavaScript
- Javascript Features
- JavaScript in HTML
- JavaScript Syntax
- Output in JavaScript
- JavaScript Variables
- Javascript let Keyword
- JS Literals and Keywords
- JavaScript Data Types
- JavaScript Operators
- JavaScript Events
- Javascript const Keyword
JAVASCRIPT FLOW CONTROL AND LOOPS
- JS if, else and else if
- JavaScript for Loop
- JS while and do-while loop
- JS Switch case
- JS break and continue
JAVASCRIPT ADVANCED
- JavaScript Strings
- JavaScript String Methods
- JavaScript Array
- JavaScript Regular Expression
- JavaScript Popup Boxes
- JavaScript Functions
- JavaScript Default Parameters
- JavaScript Rest Parameters
- JavaScript Arrow Function
- JavaScript Errors
- JavaScript Error Handling
- JavaScript Cookies
JAVASCRIPT OBJECT
- JavaScript Objects
- JavaScript User-defined Object
- JavaScript Boolean Object
- JavaScript Number Object
- JavaScript Math Object
- JavaScript Date Object
- JavaScript RegExp Object
JAVASCRIPT WEB API AND INTERFACES
JavaScript Switch case is used to execute a block of statement out of multiple blocks of statements based on a condition. JavaScript switch
is a condition-based statement that has multiple conditions but executes only one block at a time.
In switch
statement, the switch expression is evaluated only once and then the output is compared with every case mentioned inside the switch block and if the output of expression matches one of the cases then the statement written inside the block of that case is executed. If no case matches then the default case is executed.
JavaScript switch
case is used in applications where you have to implement a menu like system in which based on user input certain action is taken.
JavaScript switch
: Syntax
The expression which evaluates the condition is written inside the switch(expression), followed by a curly bracket which creates a switch block in which the different cases are defined.
switch(expression)
{
case 1:
//code
break;
case 2:
//code
break;
default:
}
Based on the value of the expression’s output, one of the many cases gets executed, and then the break
statement is called which breaks the execution, and the switch case gets exited.
NOTE: If we do not add the break
statement after the code statements in the switch case block, then the interpreter would execute each and every case after the matched case.
JavaScript Switch case: Example
In this example, we are using the switch
case and each case executes based on the provided value.
In the above example, we have used the break
statement to terminate the switch case.
JavaScript Switch Case Example without break
Use of the break
statement is optional, if we don’t use it then the switch case does not terminate and executes all the rest of the cases till the end, after the matched case. See the below example:
Switch Case Example
let a = 2; switch (a) { case 1: alert(‘case 1 has been executed’); case 2: alert(“case 2 has been executed”); case 3: alert(“case 3 has been executed”); case 4: alert(“case 4 has been executed”); default: alert(“default case executed”); }JavaScript Switch Case Default
The default statement is executed if the switch expression output does not match with any of the given cases.
Switch Default Case Example
<script>
let a = 6;
switch (a)
{
case 1: alert('case 1 has been executed');
case 2:
alert("case 2 has been executed");
case 3:
alert("case 3 has been executed");
case 4:
alert("case 4 has been executed");
default:
alert("default case executed, no matched case is found");
}
</script>
</body>
With this, we have covered the JavaScript switch statement. Although if
else
statements are popular and used in most of the cases, in some scenarios having using switch
statement makes the code more readable and scalable. So choose wisely, what to use when.