Skip to content

Commit fece590

Browse files
authored
Merge pull request #367 from brentguf/conditional-operators
Conditional operators chapter
2 parents dc79084 + cdd6c6b commit fece590

File tree

1 file changed

+10
-10
lines changed

1 file changed

+10
-10
lines changed

1-js/02-first-steps/10-ifelse/article.md

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22

33
Sometimes we need to perform different actions based on a condition.
44

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.
66

77
[cut]
88

99
## The "if" statement
1010

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.
1212

1313
For example:
1414

@@ -22,7 +22,7 @@ if (year == 2015) alert( 'You are right!' );
2222

2323
In the example above, the condition is a simple equality check: `year == 2015`, but it can be much more complex.
2424

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:
2626

2727
```js
2828
if (year == 2015) {
@@ -31,7 +31,7 @@ if (year == 2015) {
3131
}
3232
```
3333

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.
3535

3636
## Boolean conversion
3737

@@ -128,7 +128,7 @@ alert(accessAllowed);
128128

129129
The so-called "ternary" or "question mark" operator lets us do that shorter and simpler.
130130

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.
132132

133133
The syntax is:
134134
```js
@@ -151,7 +151,7 @@ Technically, we can omit parentheses around `age > 18`. The question mark operat
151151
let accessAllowed = age > 18 ? true : false;
152152
```
153153

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.
155155

156156
````smart
157157
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;
164164

165165
## Multiple '?'
166166

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.
168168

169169
For instance:
170170
```js run
@@ -201,7 +201,7 @@ if (age < 3) {
201201

202202
## Non-traditional use of '?'
203203

204-
Sometimes the question mark `'?'` is used as a replacement for `if`:
204+
Sometimes the question mark `?` is used as a replacement for `if`:
205205

206206
```js run no-beautify
207207
let company = prompt('Which company created JavaScript?', '');
@@ -212,7 +212,7 @@ let company = prompt('Which company created JavaScript?', '');
212212
*/!*
213213
```
214214

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.
216216

217217
We don't assign a result to a variable here. The idea is to execute different code depending on the condition.
218218

@@ -236,4 +236,4 @@ if (company == 'Netscape') {
236236

237237
Our eyes scan the code vertically. The constructs which span several lines are easier to understand than a long horizontal instruction set.
238238

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

Comments
 (0)