Skip to content

Commit 7f63dfd

Browse files
Add concept exercise conditionals (#1037)
Co-authored-by: Derk-Jan Karrenbeld <derk-jan+github@karrenbeld.info>
1 parent efb16e1 commit 7f63dfd

File tree

21 files changed

+866
-28
lines changed

21 files changed

+866
-28
lines changed

concepts/comparison/about.md

Lines changed: 83 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,104 @@
11
# About
22

3-
TODO: this is a stub that needs more information
3+
## Comparing Numbers
44

5-
## Comparing numbers
5+
In JavaScript numbers can be compared using the following relational and equality operators.
66

7-
Numbers are considered equal if they have the same value.
7+
| Comparison | Operator |
8+
| ---------------------- | --------- |
9+
| Greater than | `a > b` |
10+
| Greater than or equals | `a >= b` |
11+
| Less than | `a < b` |
12+
| Less than or equals | `a <= b` |
13+
| (Strict) Equals | `a === b` |
14+
| Not (strict) equals | `a !== b` |
15+
16+
The result of the comparison is always a boolean value, so either `true` or `false`.
817

918
```javascript
10-
1 == 1.0;
19+
1 < 3;
1120
// => true
1221

22+
2 !== 2;
23+
// => false
24+
1325
1 === 1.0;
1426
// => true
15-
// Remember, all numbers are floating-points, so this is different syntax for
27+
// All numbers are floating-points, so this is different syntax for
1628
// the exact same value.
29+
```
1730

18-
1 === 1n;
31+
## Comparing Strings
32+
33+
In JavaScript the comparison operators above can also be used to compare strings.
34+
In that case a dictionary (lexicographical) order is applied.
35+
You can find a list of the exact order of all the characters [here][utf-16-list].
36+
37+
```javascript
38+
'Apple' > 'Pear';
1939
// => false
20-
// Strictly checking a number against a bigint will always result in false.
2140

22-
1 == 1n;
41+
'a' < 'above';
2342
// => true
24-
// A number is equal to a bigint if they represent the same value.
2543

26-
1.0 == 1n;
44+
'a' === 'A';
45+
// => false
46+
```
47+
48+
You need to be careful when you compare two variables that appear to contain numeric values but are of type string.
49+
Due to the dictionary order the result will not be the same as comparing values of type number.
50+
51+
```javascript
52+
10 < 2;
53+
// => false
54+
55+
'10' < '2';
56+
// => true (because "1" comes before "2")
57+
```
58+
59+
Another way to compare strings is the [localeCompare][mdn-locale-compare] method.
60+
It allows to set a variety of [options][mdn-locale-compare-options] to adjust the way strings are compared.
61+
62+
## Strict Equality
63+
64+
You might wonder about the three equal signs for checking equality in JavaScript.
65+
`===` represents the check for _strict equality_ which means that no type conversion is performed and values of different types are always unequal.
66+
67+
```javascript
68+
'3' === 3;
69+
// => false
70+
// The value on the left has type string, the value on the right has type number.
71+
72+
1 === 1n;
73+
// => false
74+
// The value on the left has type number, the value on the right has type bigint.
75+
```
76+
77+
Using `===` and `!==` is the recommended way of checking equality in JavaScript.
78+
79+
## Avoiding Implicit Type Conversion
80+
81+
There is also `==` and `!=` which represents checking for _loose equality_.
82+
You should avoid it because it will apply implicit type conversion before performing the comparison.
83+
The outcomes in these cases are hard to predict and sometimes not what you would expect.
84+
You can read more about it [here][mdn-loose-equals].
85+
86+
```javascript
87+
0 == false;
2788
// => true
28-
// A number is equal to a bigint if they represent the same value.
2989
```
3090

31-
There are different outcomes when comparing numbers with different types.
32-
In general, only two operands of the type `number` can ever be _strictly equal_ (`===`), and the following can be used for _loose equality_ (`==`):
91+
In theory you can also compare values of different types (e.g., `"1" < 2`).
92+
Then the values will be implicitly converted to determine whether the result is true or false.
93+
Just as checking for loose equality, this is also not recommended for the same reason as mentioned above.
3394

34-
| A | B | `==` |
35-
| ------ | --------- | --------------------- |
36-
| Number | Undefined | `false` |
37-
| Number | Null | `false` |
38-
| Number | Number | `A === B` |
39-
| Number | String | `A === ToNumber(B)` |
40-
| Number | Boolean | `A === ToNumber(B)` |
41-
| Number | Object | `A == ToPrimitive(B)` |
95+
What should you do instead?
96+
You can apply [explicit type conversion][concept-type-conversion].
97+
With that you can then ensure values have the correct type before performing the comparison.
98+
Then your code will be easier to understand and less error prone.
4299

43-
- `ToNumber(X)` attempts to convert its argument `X` to a `number` before comparison. It is equivalent to `+B` (the unary `+` operator).
44-
- `ToPrimitive(X)` attempts to convert its object argument `X` to a primitive value, by attempting to invoke varying sequences of `X.toString` and `X.valueOf` methods on `X`.
100+
[mdn-loose-equals]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Equality
101+
[concept-type-conversion]: /tracks/javascript/concepts/type-conversion
102+
[utf-16-list]: https://www.fileformat.info/info/charset/UTF-16/list.htm
103+
[mdn-locale-compare]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/localeCompare
104+
[mdn-locale-compare-options]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/Collator/Collator#parameters
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,74 @@
11
# Introduction
2+
3+
## Comparing Numbers
4+
5+
In JavaScript numbers can be compared using the following relational and equality operators.
6+
7+
| Comparison | Operator |
8+
| ---------------------- | --------- |
9+
| Greater than | `a > b` |
10+
| Greater than or equals | `a >= b` |
11+
| Less than | `a < b` |
12+
| Less than or equals | `a <= b` |
13+
| (Strict) Equals | `a === b` |
14+
| Not (strict) equals | `a !== b` |
15+
16+
The result of the comparison is always a boolean value, so either `true` or `false`.
17+
18+
```javascript
19+
1 < 3;
20+
// => true
21+
22+
2 !== 2;
23+
// => false
24+
25+
1 === 1.0;
26+
// => true
27+
// All numbers are floating-points, so this is different syntax for
28+
// the exact same value.
29+
```
30+
31+
## Comparing Strings
32+
33+
In JavaScript the comparison operators above can also be used to compare strings.
34+
In that case a dictionary (lexicographical) order is applied.
35+
You can find a list of the exact order of all the characters [here][utf-16-list].
36+
37+
```javascript
38+
'Apple' > 'Pear';
39+
// => false
40+
41+
'a' < 'above';
42+
// => true
43+
44+
'a' === 'A';
45+
// => false
46+
```
47+
48+
## Strict Equality
49+
50+
You might wonder about the three equal signs for checking equality in JavaScript.
51+
`===` represents the check for _strict equality_ which means that no type conversion is performed and values of different types are always unequal.
52+
53+
```javascript
54+
'3' === 3;
55+
// => false
56+
// The value on the left has type string, the value on the right has type number.
57+
58+
1 === 1n;
59+
// => false
60+
// The value on the left has type number, the value on the right has type bigint.
61+
```
62+
63+
Using `===` and `!==` is the recommended way of checking equality in JavaScript.
64+
65+
There is also `==` and `!=` which represents checking for _loose equality_.
66+
You should avoid it because it will apply implicit type conversion before performing the comparison.
67+
The outcomes in these cases are hard to predict and sometimes not what you would expect.
68+
69+
```javascript
70+
0 == false;
71+
// => true
72+
```
73+
74+
[utf-16-list]: https://www.fileformat.info/info/charset/UTF-16/list.htm

concepts/comparison/links.json

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,14 @@
1-
[]
1+
[
2+
{
3+
"url": "https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators#relational_operators",
4+
"description": "MDN: Relational operators"
5+
},
6+
{
7+
"url": "https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators#equality_operators",
8+
"description": "MDN: Equality operators"
9+
},
10+
{
11+
"url": "https://www.fileformat.info/info/charset/UTF-16/list.htm",
12+
"description": "List of UTF-16 character order"
13+
}
14+
]

concepts/conditionals/about.md

Lines changed: 139 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,141 @@
11
# About
22

3-
TODO: add information on conditionals concept
3+
## General Syntax
4+
5+
A common way to conditionally execute logic in JavaScript is the if-statement.
6+
It consists of the `if` keyword, a condition wrapped in round brackets and a code block wrapped in curly brackets.
7+
The code block will only be executed if the condition evaluates to `true`.
8+
9+
```javascript
10+
if (condition) {
11+
// code that is executed if "condition" is true
12+
}
13+
```
14+
15+
It can be used stand-alone or combined with the `else` keyword.
16+
17+
```javascript
18+
if (condition) {
19+
// code that is executed if "condition" is true
20+
} else {
21+
// code that is executed otherwise
22+
}
23+
```
24+
25+
## Nested If-Statements
26+
27+
To nest another condition into the `else` statement you can use `else if`.
28+
Note that there is no `elseif` keyword in JavaScript.
29+
Instead write `else` followed by another `if` statement.
30+
31+
```javascript
32+
if (condition1) {
33+
// code that is executed if "condition1" is true
34+
} else if (condition2) {
35+
// code that is executed if "condition2" is true
36+
// but "condition1" was false
37+
} else {
38+
// code that is executed otherwise
39+
}
40+
```
41+
42+
Theoretically you can nest as many additional conditions as you want.
43+
In practice you would use a [`switch` statement](/tracks/javascript/concepts/conditionals-switch) instead in these cases.
44+
45+
```javascript
46+
if (condition1) {
47+
// ...
48+
} else if (condition2) {
49+
// ...
50+
} else if (condition3) {
51+
// ...
52+
} else if (condition4) {
53+
// ...
54+
} else {
55+
// ...
56+
}
57+
```
58+
59+
## Condition
60+
61+
When constructing complex conditions, refer to the [operator precedence table][mdn-operator-precedence] to avoid unnecessary brackets.
62+
63+
```javascript
64+
if (num >= 0 && num < 1) {
65+
// ...
66+
}
67+
68+
// The inner brackets are obsolete because relational operators
69+
// have higher precedence than logical operators.
70+
if (num >= 0 && num < 1) {
71+
// ...
72+
}
73+
```
74+
75+
Also consider using additional variables to make the code more readable.
76+
77+
```javascript
78+
const isPositive = num >= 0;
79+
const isSmall = num < 1;
80+
if (isPositive && isSmall) {
81+
// ...
82+
}
83+
```
84+
85+
In JavaScript the condition does not have to be of type boolean.
86+
If any other type than boolean is provided in a boolean context like the if-statement, JavaScript will implicitly convert the value to boolean.
87+
Refer to the [type coercion concept][concept-type-coercion] for details on which values are _truthy_ and _falsy_, respectively.
88+
89+
```javascript
90+
const num = 4;
91+
if (num) {
92+
// this code block will be executed because 4 is truthy
93+
}
94+
```
95+
96+
## Short-Hand Notations
97+
98+
If you only want to execute one statement in the code block for `if` or `else`, it is possible in JavaScript to omit the curly brackets.
99+
100+
<!-- prettier-ignore-start -->
101+
```javascript
102+
if (condition) doSomething();
103+
104+
// or
105+
106+
if (condition)
107+
doSomething();
108+
```
109+
<!-- prettier-ignore-end -->
110+
111+
This is sometimes used when checking for an error condition for example.
112+
In general it is not recommended because it is easy to forget to add the brackets back in when adding a second statement that should depend on the same condition.
113+
114+
When writing functions, it is a common pattern to omit the `else` block and use an early `return` in the `if` block instead.
115+
In many cases this reduces _nesting_ and makes the code more readable and easier to follow.
116+
117+
```javascript
118+
function checkNumber(num) {
119+
let message = '';
120+
121+
if (num === 0) {
122+
message = 'You passed 0, please provide another number.';
123+
} else {
124+
message = 'Thanks for passing such a nice number.';
125+
}
126+
127+
return message;
128+
}
129+
130+
// Can also be written as ...
131+
function checkNumber(num) {
132+
if (num === 0) {
133+
return 'You passed 0, please provide another number.';
134+
}
135+
136+
return 'Thanks for passing such a nice number.';
137+
}
138+
```
139+
140+
[concept-type-coercion]: /tracks/javascript/concepts/type-coercion
141+
[mdn-operator-precedence]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Operator_Precedence#table
Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,34 @@
11
# Introduction
22

3-
TODO: add introduction for conditionals concept
3+
A common way to conditionally execute logic in JavaScript is the if-statement.
4+
It consists of the `if` keyword, a condition wrapped in round brackets and a code block wrapped in curly brackets.
5+
The code block will only be executed if the condition evaluates to `true`.
6+
7+
```javascript
8+
if (condition) {
9+
// code that is executed if "condition" is true
10+
}
11+
```
12+
13+
It can be used stand-alone or combined with the `else` keyword.
14+
15+
```javascript
16+
if (condition) {
17+
// code that is executed if "condition" is true
18+
} else {
19+
// code that is executed otherwise
20+
}
21+
```
22+
23+
To nest another condition into the `else` statement you can use `else if`.
24+
25+
```javascript
26+
if (condition1) {
27+
// code that is executed if "condition1" is true
28+
} else if (condition2) {
29+
// code that is executed if "condition2" is true
30+
// but "condition1" was false
31+
} else {
32+
// code that is executed otherwise
33+
}
34+
```

0 commit comments

Comments
 (0)