GeezerVison is showing github.com — Add article about else · Purukitto/javascript@d65d375
File tree
Expand file treeCollapse file tree
Open diff view settings
Expand file treeCollapse file tree
Open diff view settings
Collapse file
Copy file name to clipboard +56Lines changed: 56 additions & 0 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
|
| 1 | +# Else |
| 2 | + |
| 3 | +There is also an `else` clause that will be applied every time the first condition isn’t true. This is very powerful if you want to react to any value, but single out one in particular for special treatment: |
| 4 | + |
| 5 | +```javascript |
| 6 | +var umbrellaMandatory; |
| 7 | + |
| 8 | +if(country === 'England'){ |
| 9 | + umbrellaMandatory = true; |
| 10 | +} else { |
| 11 | + umbrellaMandatory = false; |
| 12 | +} |
| 13 | +``` |
| 14 | + |
| 15 | +The `else` clause can be join with another `if`, lets remake the example from the previous article: |
| 16 | + |
| 17 | +```javascript |
| 18 | +if(country === 'England') { |
| 19 | + ... |
| 20 | +} else if(country === 'France') { |
| 21 | + ... |
| 22 | +} else if(country === 'Germany') { |
| 23 | + ... |
| 24 | +} |
| 25 | +``` |
| 26 | + |
| 27 | + |
| 28 | +--- |
| 29 | + |
| 30 | +Fill up the value of `name` to validate the `else` condition. |
| 31 | + |
| 32 | +```js |
| 33 | +var name = |
| 34 | + |
| 35 | +if (name == "John") { |
| 36 | + |
| 37 | +} else if (name == "Aaron") { |
| 38 | + // Valid this condition |
| 39 | +} |
| 40 | +``` |
| 41 | + |
| 42 | +```js |
| 43 | +var name = "Aaron"; |
| 44 | + |
| 45 | +if (name == "John") { |
| 46 | + |
| 47 | +} else if (name == "Aaron") { |
| 48 | + // Valid this condition |
| 49 | +} |
| 50 | +``` |
| 51 | + |
| 52 | +```js |
| 53 | +assert(name == "Aaron"); |
| 54 | +``` |
| 55 | + |
| 56 | +--- |
Collapse file
Copy file name to clipboardExpand all lines: conditional/if.md +3-3Lines changed: 3 additions & 3 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
@@ -8,19 +8,19 @@ var weather; |
8 | 8 | var food; |
9 | 9 | var currency; |
10 | 10 |
|
11 | | -if(country === 'England'){ |
| 11 | +if(country === 'England') { |
12 | 12 | weather = 'horrible'; |
13 | 13 | food = 'filling'; |
14 | 14 | currency = 'pound sterling'; |
15 | 15 | } |
16 | 16 |
|
17 | | -if(country === 'France'){ |
| 17 | +if(country === 'France') { |
18 | 18 | weather = 'nice'; |
19 | 19 | food = 'stunning, but hardly ever vegetarian'; |
20 | 20 | currency = 'funny, small and colourful'; |
21 | 21 | } |
22 | 22 |
|
23 | | -if(country === 'Germany'){ |
| 23 | +if(country === 'Germany') { |
24 | 24 | weather = 'average'; |
25 | 25 | food = 'wurst thing ever'; |
26 | 26 | currency = 'funny, small and colourful'; |
|