mirror of
https://github.com/microsoft/Web-Dev-For-Beginners.git
synced 2025-08-14 10:44:57 +02:00
added switch statement
This commit is contained in:
committed by
Sara Gibbons
parent
500c13ed3e
commit
6cbe6aa8b9
@@ -81,6 +81,45 @@ else{
|
||||
|
||||
✅ Test your understanding of this code and the following code by running it in a browser console. Change the values of the currentMoney and laptopPrice variables to change the returned `console.log()`.
|
||||
|
||||
|
||||
## Switch Statement
|
||||
|
||||
The `switch` statement is used to perform different actions based on different conditions.Use the `switch` statement to select one of many code blocks to be executed.
|
||||
|
||||
```javascript
|
||||
switch(expression) {
|
||||
case x:
|
||||
// code block
|
||||
break;
|
||||
case y:
|
||||
// code block
|
||||
break;
|
||||
default:
|
||||
// code block
|
||||
}
|
||||
```
|
||||
|
||||
```javascript
|
||||
// program using switch statement
|
||||
let a = 2;
|
||||
|
||||
switch (a) {
|
||||
|
||||
case 1:
|
||||
a = 'one';
|
||||
break;
|
||||
case 2:
|
||||
a = 'two';
|
||||
break;
|
||||
default:
|
||||
a = 'not found';
|
||||
break;
|
||||
}
|
||||
console.log(`The value is ${a}`);
|
||||
```
|
||||
✅ Test your understanding of this code and the following code by running it in a browser console. Change the values of the varaiable a to change the returned `console.log()`.
|
||||
|
||||
|
||||
## Logical Operators and Booleans
|
||||
|
||||
Decisions might require more than one comparison, and can be strung together with logical operators to produce a Boolean value.
|
||||
|
Reference in New Issue
Block a user