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
+11-11Lines changed: 11 additions & 11 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,14 +1,14 @@
1
1
# Conditional operators: if, '?'
2
2
3
-
Sometimes we need to perform different actions basing on a condition.
3
+
Sometimes we need to perform different actions based on a condition.
4
4
5
-
There's an`if`operator for that and also the "question mark" operator: `"?"` for conditional evaluation.
5
+
There is`if`statement for that and also the conditional (ternary) operator for conditional evaluation which we will be referring as “question mark” operator: "?" for simplicity.
6
6
7
7
[cut]
8
8
9
-
## The "if" operator
9
+
## The "if" statement
10
10
11
-
The "if" operator 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's more than one command to execute -- we can use a code block in figure brackets:
25
+
If there is more than one command to execute -- we can use a code block in figure brackets:
26
26
27
27
```js
28
28
if (year ==2015) {
@@ -31,11 +31,11 @@ if (year == 2015) {
31
31
}
32
32
```
33
33
34
-
It is recommended to use figure brackets every time with `if`, even if there's only one command. That improves readability.
34
+
It is recommended to use figure brackets every time with `if`, even if there is only one command. That improves readability.
35
35
36
36
## Boolean conversion
37
37
38
-
The `if (…)`operator evaluates the expression in parentheses and converts it to the boolean type.
38
+
The `if (…)`statement evaluates the expression in parentheses and converts it to the boolean type.
39
39
40
40
Let's recall the conversion rules from the chapter <info:type-conversions>:
41
41
@@ -70,7 +70,7 @@ if (cond) {
70
70
71
71
## The "else" clause
72
72
73
-
The `if`operator may contain an optional "else" block. It executes when the condition is wrong.
73
+
The `if`statement may contain an optional "else" block. It executes when the condition is wrong.
74
74
75
75
For example:
76
76
```js run
@@ -85,7 +85,7 @@ if (year == 2015) {
85
85
86
86
## Several conditions: "else if"
87
87
88
-
Sometimes we'd like to test several variants of a condition. There's an `else if` clause for that.
88
+
Sometimes we'd like to test several variants of a condition. There is an `else if` clause for that.
89
89
90
90
For example:
91
91
@@ -220,7 +220,7 @@ We don't assign a result to a variable here, the idea is to execute different co
220
220
221
221
The notation seem to be shorter than `if`, that appeals to some programmers. But it is less readable.
222
222
223
-
Here's the same with `if` for comparison:
223
+
Here is the same with `if` for comparison:
224
224
225
225
```js run no-beautify
226
226
let company =prompt('Which company created JavaScript?', '');
@@ -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's`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