You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: 1-js/02-first-steps/10-ifelse/article.md
+10-10Lines changed: 10 additions & 10 deletions
Original file line number
Diff line number
Diff line change
@@ -2,13 +2,13 @@
2
2
3
3
Sometimes we need to perform different actions based on a condition.
4
4
5
-
There is the `if` statement for that and also the conditional (ternary) operator for conditional evaluation which we will be referring as the “question mark” operator: `"?"` for simplicity.
5
+
There is the `if` statement for that and also the conditional (ternary) operator for conditional evaluation which we will be referring as the “question mark” operator`?` for simplicity.
6
6
7
7
[cut]
8
8
9
9
## The "if" statement
10
10
11
-
The "if" statement gets a condition, evaluates it and, if the result is `true`, executes the code.
11
+
The `if` statement gets a condition, evaluates it and, if the result is `true`, executes the code.
12
12
13
13
For example:
14
14
@@ -22,7 +22,7 @@ if (year == 2015) alert( 'You are right!' );
22
22
23
23
In the example above, the condition is a simple equality check: `year == 2015`, but it can be much more complex.
24
24
25
-
If there is more than one command to execute, we can use a code block in figure brackets:
25
+
If there is more than one statement to be executed, we have to wrap our code block inside curly braces:
26
26
27
27
```js
28
28
if (year ==2015) {
@@ -31,7 +31,7 @@ if (year == 2015) {
31
31
}
32
32
```
33
33
34
-
It is recommended to use figure brackets every time with `if`, even if there is only one command. That improves readability.
34
+
It is recommended to wrap your code block with curly braces `{}`every time with `if`, even if there is only one statement. That improves readability.
35
35
36
36
## Boolean conversion
37
37
@@ -128,7 +128,7 @@ alert(accessAllowed);
128
128
129
129
The so-called "ternary" or "question mark" operator lets us do that shorter and simpler.
130
130
131
-
The operator is represented by a question mark `"?"`. The formal term "ternary" means that the operator has three operands. It is actually the one and only operator in JavaScript which has that many.
131
+
The operator is represented by a question mark `?`. The formal term "ternary" means that the operator has three operands. It is actually the one and only operator in JavaScript which has that many.
132
132
133
133
The syntax is:
134
134
```js
@@ -151,7 +151,7 @@ Technically, we can omit parentheses around `age > 18`. The question mark operat
151
151
let accessAllowed = age >18?true:false;
152
152
```
153
153
154
-
...But parentheses make the code more readable. So it's recommended to use them.
154
+
But parentheses make the code more readable, so it's recommended to use them.
155
155
156
156
````smart
157
157
In the example above it's possible to evade the question mark operator, because the comparison by itself returns `true/false`:
@@ -164,7 +164,7 @@ let accessAllowed = age > 18;
164
164
165
165
## Multiple '?'
166
166
167
-
A sequence of question mark `"?"` operators allows returning a value that depends on more than one condition.
167
+
A sequence of question mark `?` operators allows returning a value that depends on more than one condition.
168
168
169
169
For instance:
170
170
```js run
@@ -201,7 +201,7 @@ if (age < 3) {
201
201
202
202
## Non-traditional use of '?'
203
203
204
-
Sometimes the question mark `'?'` is used as a replacement for `if`:
204
+
Sometimes the question mark `?` is used as a replacement for `if`:
205
205
206
206
```js run no-beautify
207
207
let company =prompt('Which company created JavaScript?', '');
@@ -212,7 +212,7 @@ let company = prompt('Which company created JavaScript?', '');
212
212
*/!*
213
213
```
214
214
215
-
Depending on the condition `company == 'Netscape'`, either the first or the second part after `"?"` gets executed and shows the alert.
215
+
Depending on the condition `company == 'Netscape'`, either the first or the second part after `?` gets executed and shows the alert.
216
216
217
217
We don't assign a result to a variable here. The idea is to execute different code depending on the condition.
218
218
@@ -236,4 +236,4 @@ if (company == 'Netscape') {
236
236
237
237
Our eyes scan the code vertically. The constructs which span several lines are easier to understand than a long horizontal instruction set.
238
238
239
-
The idea of a question mark `'?'` is to return one or another value depending on the condition. Please use it for exactly that. There is `if` to execute different branches of the code.
239
+
The idea of a question mark `?` is to return one or another value depending on the condition. Please use it for exactly that. There is `if` to execute different branches of the code.
0 commit comments