|
| 1 | +# Conditional execution |
| 2 | + |
| 3 | +The normal order of execution of statements in a computer program is in straight-line order, from top to bottom. However, sometimes it is desirable to execute one or more statements _conditionally_, i.e. depending on whether some condition – determined by the state of your program – holds true. |
| 4 | + |
| 5 | +## The `if` statement |
| 6 | + |
| 7 | +In its simplest form the `if` statement looks like this: |
| 8 | + |
| 9 | +```js |
| 10 | +if (condition) { |
| 11 | + // one or more statements that will be executed |
| 12 | + // if, and only if the condition holds true |
| 13 | +} |
| 14 | +``` |
| 15 | + |
| 16 | +Here, `condition` is a boolean expression that resolves to either `true` or `false` (or, more precisely, any expression that is 'truthy' or 'falsy', as will be explained later). |
| 17 | + |
| 18 | +The statements within the curly braces `{` and `}` will be executed if the condition holds true, otherwise these statements will be skipped (i.e. ignored). |
| 19 | + |
| 20 | +An example: |
| 21 | + |
| 22 | +```js |
| 23 | +if (distance < 10) { |
| 24 | + console.log('I will take the bike.'); |
| 25 | +} |
| 26 | +``` |
| 27 | + |
| 28 | +Here, the condition is the boolean expression `distance > 10`, which is either `true` or `false`. |
| 29 | + |
| 30 | +It is also possible to add a statement block to be executed if (and only if) the condition does **not** hold true, using an `else` clause. |
| 31 | + |
| 32 | +```js |
| 33 | +if (distance < 10) { |
| 34 | + console.log('I will take the bike.'); |
| 35 | +} else { |
| 36 | + console.log('I will go by car.'); |
| 37 | +} |
| 38 | +``` |
| 39 | + |
| 40 | +A condition can take more complex forms, using `&&` (logival AND) and `||` (logical OR) operators: |
| 41 | + |
| 42 | +```js |
| 43 | +if (distance < 10 && !raining) { |
| 44 | + console.log('I will take the bike.'); |
| 45 | +} else { |
| 46 | + console.log('I will go by car.'); |
| 47 | +} |
| 48 | +``` |
| 49 | + |
| 50 | +In the example above `raining` is a boolean variable (either `true` or `false`), and the exclamation mark is the logical NOT operator that negates the boolean value (if it was `true` the result after negation is false and vice versa). |
| 51 | + |
| 52 | +For more complex decisions you can concatenate multiple conditions using `else if` clauses. |
| 53 | + |
| 54 | +```js |
| 55 | +if (distance < 1) { |
| 56 | + console.log('I will walk.'); |
| 57 | +} else if (distance < 10) { |
| 58 | + console.log('I will take the bike.'); |
| 59 | +} else if (distance < 50) { |
| 60 | + console.log('I will go by car.'); |
| 61 | +} else { |
| 62 | + console.log('I will take the train.'); |
| 63 | +} |
| 64 | +``` |
| 65 | + |
| 66 | +The statement block inside an `if`, `else` or `else if` may itself contain nested `if` statements, as in this example: |
| 67 | + |
| 68 | +```js |
| 69 | +if (distance < 10) { |
| 70 | + if (raining) { |
| 71 | + console.log('I will go public transportation.'); |
| 72 | + } else { |
| 73 | + console.log('I will walk.'); |
| 74 | + } |
| 75 | +} else { |
| 76 | + console.log('I will go by car.'); |
| 77 | +} |
| 78 | +``` |
| 79 | + |
| 80 | +> As (nested) `if` statements can become quite complex it is very important that you indent your source code so that there can be no confusion about which statement blocks are executed for each condition, as was done in the examples. |
| 81 | +
|
| 82 | +>More informaton on MDN: [if...else](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/if...else) |
| 83 | +
|
| 84 | +## The conditional (ternary) operator |
| 85 | + |
| 86 | +This operator can be used as a shortcut for an `if` statement when dealing with expressions. |
| 87 | + |
| 88 | +The general format is: |
| 89 | + |
| 90 | +```js |
| 91 | +condition ? expr1 : expr2 |
| 92 | +``` |
| 93 | + |
| 94 | +('ternary' means: _composed of three parts_) |
| 95 | + |
| 96 | +It is often used in combination with an assignment, as in this example: |
| 97 | + |
| 98 | +```js |
| 99 | +const conditionOfCar = age < 1 ? 'new' : 'used'; |
| 100 | +``` |
| 101 | + |
| 102 | +The `conditionOfCar` variable will be assigned the string `'new'` if the `age < 1` condition holds true, otherwise it is assigned the string `'used'`. |
| 103 | + |
| 104 | +It is **not** recommended to use the conditional operator if you do not intend to use its value: |
| 105 | + |
| 106 | +```js |
| 107 | +// Don't do this: it's yucky code |
| 108 | +age < 1 ? console.log('new') : console.log('used'); |
| 109 | +``` |
| 110 | + |
| 111 | +>More informaton on MDN: [Conditional (ternary) Operator](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Conditional_Operator) |
| 112 | +
|
| 113 | +## The switch statement |
| 114 | + |
| 115 | +The `switch` statement can sometimes be a useful alternative to a concatenation of `if` statements. This is the case when the condition is an expression that can be decomposed into a number of distinct values or _cases_, as shown in the example below. |
| 116 | + |
| 117 | +```js |
| 118 | +const hyfModule = 'JavaScript-1'; |
| 119 | + |
| 120 | +switch (hyfModule) { |
| 121 | + case 'HTML/CSS': |
| 122 | + console.log('In this module you will learn HTML and CSS.'); |
| 123 | + break; |
| 124 | + case 'JavaScript-1': |
| 125 | + console.log('In this module you will learn Git and JavaScript basics.'); |
| 126 | + break; |
| 127 | + case 'JavaScript-2': |
| 128 | + console.log('In this module you will learn about JavaScript in the browser with HTML and CSS.'); |
| 129 | + break; |
| 130 | + case 'JavaScript-3': |
| 131 | + console.log('In this module you will learn about Async and API calls.'); |
| 132 | + break; |
| 133 | + case 'Node': |
| 134 | + console.log('This module is about building server and CLI applications using Node.'); |
| 135 | + break; |
| 136 | + case 'Database': |
| 137 | + console.log('In this module is about Relational and Non-Relational Data and Database Systems.'); |
| 138 | + break; |
| 139 | + case 'React': |
| 140 | + console.log('In this module you will to build Single Page Applications using React.'); |
| 141 | + break; |
| 142 | + case 'Project': |
| 143 | + console.log('In this final module you will do your graduation project.'); |
| 144 | + break; |
| 145 | + default: |
| 146 | + console.log('This module is unknown: ' + hyfModule); |
| 147 | +}} |
| 148 | +``` |
| 149 | + |
| 150 | +Depending on the value of the expression specified in the `switch` clause, one of the `case` statement blocks is executed. Each statement block should end with a `break` statement to ensure that a `case` doesn't 'fall through' into the next `case`. |
| 151 | + |
| 152 | +The `default` statement at the end is executed when none of the preceding cases hold true. The `default` statement is not strictly required, but is a best practice to always specify one. |
| 153 | + |
| 154 | +>More informaton on MDN: [switch](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/switch) |
| 155 | +
|
| 156 | +## truthy, falsy |
| 157 | + |
| 158 | +**truthy**: 'sort of' `true` |
| 159 | +**falsy**: 'sort of' `false` |
| 160 | + |
| 161 | +From MDN: |
| 162 | + |
| 163 | +In JavaScript, a **truthy** value is a value that is considered true when evaluated in a Boolean context. All values are truthy unless they are defined as **falsy**. |
| 164 | + |
| 165 | +**falsy** values are: |
| 166 | + |
| 167 | +- `false` |
| 168 | +- `0` |
| 169 | +- `""` |
| 170 | +- `null` |
| 171 | +- `undefined` |
| 172 | +- `NaN` |
| 173 | + |
| 174 | +The example below will print `x is undefined` because `undefined` is **falsy**. |
| 175 | + |
| 176 | +```js |
| 177 | +let x; |
| 178 | +if (x) { |
| 179 | + console.log('x is defined'); |
| 180 | +} else { |
| 181 | + console.log('x is undefined'); |
| 182 | +} |
| 183 | +``` |
| 184 | + |
| 185 | +>More informaton on MDN: [Truthy](https://developer.mozilla.org/en-US/docs/Glossary/Truthy), [Falsy](https://developer.mozilla.org/en-US/docs/Glossary/Falsy) |
0 commit comments