Skip to content
This repository has been archived by the owner on Apr 1, 2021. It is now read-only.

Commit

Permalink
Merge pull request #1204 from dakshshah96/add/js-switch
Browse files Browse the repository at this point in the history
Add JS Article Switch
  • Loading branch information
Rafase282 authored Jul 1, 2016
2 parents 7c3a5df + 808e8c4 commit f533c16
Showing 1 changed file with 77 additions and 0 deletions.
77 changes: 77 additions & 0 deletions JS-Switch.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
# JavaScript Switch

## Introduction

The **switch** statement evaluates an expression, matching the expression's value to a `case` clause, and executes statements associated with that case.

```javascript
switch (expression) {
case value1:
// statements executed when the result of expression matches value1
break;
case value2:
// statements executed when the result of expression matches value2
break;
...
case valueN:
// statements executed when the result of expression matches valueN
break;
default:
// statements executed when none of the values match the value of the expression
break;
}
```

The optional **break** statement associated with each case label ensures that the program breaks out of `switch` once the matched statement is executed and continues execution at the statement following switch. If **break** is omitted, the program continues execution at the next statement in the `switch` statement.

[MDN link](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/switch) | [MSDN link](https://msdn.microsoft.com/en-us/library/hzc6t81t.aspx)

## Examples

In the following example, if **expr** evaluates to "Bananas", the program matches the value with case "Bananas" and executes the associated statement. When **break** is encountered, the program breaks out of `switch` and executes the statement following `switch`. If **break** were omitted, the statement for case "Cherries" would also be executed.

```javascript
switch (expr) {
case "Oranges":
console.log("Oranges are $0.59 a pound.");
break;
case "Apples":
console.log("Apples are $0.32 a pound.");
break;
case "Bananas":
console.log("Bananas are $0.48 a pound.");
break;
case "Cherries":
console.log("Cherries are $3.00 a pound.");
break;
case "Mangoes":
case "Papayas":
console.log("Mangoes and papayas are $2.79 a pound.");
break;
default:
console.log("Sorry, we are out of " + expr + ".");
}

console.log("Is there anything else you'd like?");
```

:rocket: [Run Code](https://repl.it/C8DP/0)

The following example takes advantage of the fact that if there is no **break** below a case statement it will continue to execute the next case statement regardless if the case meets the criteria. Here, four different values ("Cow", "Giraffe", "Dog", "Pig") perform exactly the same operation.

```javascript
var Animal = 'Giraffe';
switch (Animal) {
case 'Cow':
case 'Giraffe':
case 'Dog':
case 'Pig':
console.log('This animal will go on Noah\'s Ark.');
break;
case 'Dinosaur':
default:
console.log('This animal will not.');
}
```

:rocket: [Run Code](https://repl.it/C8DP/1)

0 comments on commit f533c16

Please sign in to comment.