GeezerVison is showing github.com — Add article about conditional comparators · op2146/javascript@b35db41
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 +46Lines changed: 46 additions & 0 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
|
| 1 | +# Comparators |
| 2 | + |
| 3 | +Lets now focus on the conditional part: |
| 4 | + |
| 5 | +```javascript |
| 6 | +if (country === "France") { |
| 7 | + ... |
| 8 | +} |
| 9 | +``` |
| 10 | + |
| 11 | +The conditional part is the country followed by the three equal signs (`===`). Three equal signs mean that the condition tests if the variable country has the value you test against but also the correct variable (data)type. You can test conditions with double equal signs, too, but that would mean that `if(x == 5)` would be true for x being 5 and x being “5”. Depending on what your program is doing, this could make quite a difference. |
| 12 | + |
| 13 | +Other conditional test: |
| 14 | + |
| 15 | +* ```x > a```: is x bigger than a? |
| 16 | +* ```x < a```: is x less than a? |
| 17 | +* ```x <= a```: is x less than or equal to a? |
| 18 | +* ```x != a```: is x not a? |
| 19 | +* ```x```: does x exist? |
| 20 | + |
| 21 | + |
| 22 | +--- |
| 23 | + |
| 24 | +Add a condition to change the value of `a` to 10 if `x` is bigger than 5. |
| 25 | + |
| 26 | +```js |
| 27 | +var x = 6; |
| 28 | +var a = 0; |
| 29 | + |
| 30 | + |
| 31 | +``` |
| 32 | + |
| 33 | +```js |
| 34 | +var x = 6; |
| 35 | +var a = 0; |
| 36 | + |
| 37 | +if (x > 5) { |
| 38 | + a = 10; |
| 39 | +} |
| 40 | +``` |
| 41 | + |
| 42 | +```js |
| 43 | +assert(a == 10); |
| 44 | +``` |
| 45 | + |
| 46 | +--- |