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/07-operators/article.md
+9-9Lines changed: 9 additions & 9 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,6 +1,6 @@
1
1
# Operators
2
2
3
-
Many operators are known to us from school. It is an addition `+`, a multiplication `*`, a substraction`-` and so on.
3
+
Many operators are known to us from school. It is an addition `+`, a multiplication `*`, a subtraction`-` and so on.
4
4
5
5
In this chapter we concentrate on aspects that are not covered by the school arithmetic.
6
6
@@ -11,24 +11,24 @@ In this chapter we concentrate on aspects that are not covered by the school ari
11
11
Before we move on, let's grasp the common terminology.
12
12
13
13
-*An operand* -- is what operators are applied to. For instance in multiplication `5 * 2` there are two operands: the left operand is `5`, and the right operand is `2`. Sometimes people say "arguments" instead of "operands".
14
-
- An operator is *unary* if it has a single operand. For example, the unary minus`"-"` reverses the sign of the number:
14
+
- An operator is *unary* if it has a single operand. For example, the unary negation`"-"` reverses the sign of the number:
15
15
16
16
```js run
17
17
let x =1;
18
18
19
19
*!*
20
20
x =-x;
21
21
*/!*
22
-
alert( x ); // -1, unary minus was applied
22
+
alert( x ); // -1, unary negation was applied
23
23
```
24
24
- An operator is *binary*if it has two operands. The same minus exists in the binary form as well:
25
25
26
26
```js run no-beautify
27
27
let x = 1, y = 3;
28
-
alert( y - x ); // 2, binary minus substracts values
28
+
alert( y - x ); // 2, binary minus subtracts values
29
29
```
30
30
31
-
Formally, we're talking about the two different operators here: the unary minus (single operand, reverses the sign) and the binary minus (two operands, substracts).
31
+
Formally, we're talking about the two different operators here: the unary negation (single operand, reverses the sign) and the binary subtraction (two operands, subtracts).
32
32
33
33
## Strings concatenation, binary +
34
34
@@ -135,11 +135,11 @@ An extract from the [precedence table](https://developer.mozilla.org/en/JavaScri
135
135
| Precedence | Name | Sign |
136
136
|------------|------|------|
137
137
|...|...|...|
138
-
|15| unary plus |`+`|
139
-
|15| unary minus|`-`|
138
+
|16| unary plus |`+`|
139
+
|16| unary negation|`-`|
140
140
|14| multiplication |`*`|
141
141
|14| division |`/`|
142
-
|13|addition(binary) |`+`|
142
+
|13| addition |`+`|
143
143
|13| subtraction |`-`|
144
144
|...|...|...|
145
145
|3| assignment |`=`|
@@ -194,7 +194,7 @@ alert( a ); // 3
194
194
alert( c ); // 0
195
195
```
196
196
197
-
In the example above, the result of `(a = b + 1)` is the value which is assigned to `a` (that is `3`). It is then used to substract from `3`.
197
+
In the example above, the result of `(a = b + 1)` is the value which is assigned to `a` (that is `3`). It is then used to subtract from `3`.
198
198
199
199
Funny code, isn't it? We should understand how it works, because sometimes we can see it in3rd-party libraries, but shouldn't write anything like that ourselves. Such tricks definitely don't make the code clearer and readable.
Copy file name to clipboardExpand all lines: 1-js/02-first-steps/09-alert-prompt-confirm/article.md
+2-2Lines changed: 2 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -22,7 +22,7 @@ For example:
22
22
alert("Hello");
23
23
```
24
24
25
-
The mini-window with the message is called a *modal window*. The word "modal" means that the visitor can't interact with the rest of the page, press other buttons etc, until they deal with the window. In this case -- until they press "OK".
25
+
The mini-window with the message is called a *modal window*. The word "modal" means that the visitor can't interact with the rest of the page, press other buttons etc, until they have dealt with the window. In this case -- until they press "OK".
26
26
27
27
## prompt
28
28
@@ -101,7 +101,7 @@ We covered 3 browser-specific functions to interact with the visitor:
101
101
`confirm`
102
102
: shows a message and waits the user to press "OK" or "CANCEL". It returns `true` for OK and `false` for CANCEL/`key:Esc`.
103
103
104
-
All these methods are modal: they pause the script execution and don't let the visitor to interact with the rest of the page until he dismisses them.
104
+
All these methods are modal: they pause the script execution and don't allow the visitor to interact with the rest of the page until the message has been dismissed.
105
105
106
106
There are two limitations shared by all the methods above:
Copy file name to clipboardExpand all lines: 1-js/02-first-steps/10-ifelse/article.md
+2-2Lines changed: 2 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -126,7 +126,7 @@ if (age > 18) {
126
126
alert(accessAllowed);
127
127
```
128
128
129
-
The so-called "ternary" or "question mark" operator allows to do that shorter and simpler.
129
+
The so-called "ternary" or "question mark" operator lets us do that shorter and simpler.
130
130
131
131
The operator is represented by a question mark `"?"`. The formal term "ternary" means that the operator has 3 operands. It is actually the one and only operator in JavaScript which has that many.
132
132
@@ -143,7 +143,7 @@ For example:
143
143
let accessAllowed = (age >18) ?true:false;
144
144
```
145
145
146
-
Technically, we can omit parentheses around `age > 14`. The question mark operator has a low precedence. It executes after the comparison `>`, so that'll do the same:
146
+
Technically, we can omit parentheses around `age > 18`. The question mark operator has a low precedence. It executes after the comparison `>`, so that'll do the same:
147
147
148
148
```js
149
149
// the comparison operator "age > 18" executes first anyway
Copy file name to clipboardExpand all lines: 1-js/02-first-steps/11-logical-operators/article.md
+18-18Lines changed: 18 additions & 18 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -16,7 +16,7 @@ The "OR" operator is represented with two vertical line symbols:
16
16
result = a || b;
17
17
```
18
18
19
-
In classical programming, logical OR is meant to manipulate boolean values. If any of it's arguments is`true`, then it returns `true`, otherwise -- returns `false`.
19
+
In classical programming, logical OR is meant to manipulate boolean values only. If any of its arguments are`true`, then it returns `true`, otherwise it returns `false`.
20
20
21
21
In JavaScript the operator is a little bit more tricky and powerful. But first let's see what happens with boolean values.
22
22
@@ -62,7 +62,7 @@ let hour = 12;
62
62
let isWeekend =true;
63
63
64
64
if (hour <10|| hour >18|| isWeekend) {
65
-
alert( 'The office is closed.' ); // it is weekend
65
+
alert( 'The office is closed.' ); // it is the weekend
66
66
}
67
67
```
68
68
@@ -78,13 +78,13 @@ Given multiple OR'ed values:
78
78
result = value1 || value2 || value3;
79
79
```
80
80
81
-
The OR `"||"` operator is doing the following:
81
+
The OR `"||"` operator does the following:
82
82
83
-
-Evalutes operands from left to right.
84
-
- For each value -- converts it to boolean. If it's true then stops and returns that value.
85
-
- If operands finished, returns the last value.
83
+
-Evalute operands from left to right.
84
+
- For each operand, convert it to boolean. If the result is `true`, then stop and return that the original value of that operand.
85
+
- If all other operands have been assessed (i.e. all were `falsy`), return the last operand.
86
86
87
-
A value is returned in it's original form, without the conversion.
87
+
A value is returned in its original form, without the conversion.
88
88
89
89
In other words, a chain of OR `"||"` returns the first truthy value or the last one if no such value found.
90
90
@@ -159,7 +159,7 @@ The AND operator is represented with two ampersands `&&`:
159
159
result = a && b;
160
160
```
161
161
162
-
In classical programming AND returns `true`if both operands are truthy and `false`--otherwise:
162
+
In classical programming AND returns `true`if both operands are truthy and `false` otherwise:
163
163
164
164
```js run
165
165
alert( true && true ); // true
@@ -196,26 +196,26 @@ Given multiple AND'ed values:
196
196
result = value1 && value2 && value3;
197
197
```
198
198
199
-
The AND `"&&"` operator is doing the following:
199
+
The AND `"&&"` operator does the following:
200
200
201
-
- Evalutes operands from left to right.
202
-
- For each value converts it to a boolean. If the result is `false`, stops and returns the original value.
203
-
- If values finished (all are truthy), returns the last one.
201
+
- Evalute operands from left to right.
202
+
- For each operand, convert it to a boolean. If the result is `false`, stop and return the original value of that operand.
203
+
- If all other operands have been assessed (i.e. all were truthy), return the last operand.
204
204
205
-
In other words, AND returns the first falsy value or the last value if none found.
205
+
In other words, AND returns the first falsy value or the last value if none were found.
206
206
207
207
The rules above are similar to OR. The difference is that AND returns the first *falsy* value while OR returns the first *truthy* one.
208
208
209
209
Examples:
210
210
211
211
```js run
212
212
// if the first operand is truthy,
213
-
// AND returns the second one:
213
+
// AND returns the second operand:
214
214
alert( 1 && 0 ); // 0
215
215
alert( 1 && 5 ); // 5
216
216
217
217
// if the first operand is falsy,
218
-
// AND returns it, and the second one is ignored
218
+
// AND returns it. The second operand is ignored
219
219
alert( null && 5 ); // null
220
220
alert( 0 && "no matter what" ); // 0
221
221
```
@@ -266,7 +266,7 @@ if (x > 0) {
266
266
267
267
The variant with `&&` appears to be shorter. But `if` is more obvious and tends to be a little bit more readable.
268
268
269
-
So it is recommended to use every construct for it'spurpose. Use`if`if we want if. And use `&&`if we want AND.
269
+
So it is recommended to use every construct for its purpose. Use `if` if we want if. And use `&&` if we want AND.
That is: the first NOT converts the value to boolean and returns the inverse, and the second NOT inverses it again, at the end we have a plain value-to-boolean conversion.
300
+
That is, the first NOT converts the value to boolean and returns the inverse, and the second NOT inverses it again. At the end we have a plain value-to-boolean conversion.
301
301
302
-
There's a little more verbose to do the same -- a built-in `Boolean` function:
302
+
There's a little more verbose way to do the same thing-- a built-in`Boolean` function:
Copy file name to clipboardExpand all lines: 1-js/02-first-steps/12-while-for/article.md
+4-4Lines changed: 4 additions & 4 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -142,7 +142,7 @@ if (i < 3) { alert(i); i++ }
142
142
```
143
143
144
144
````smart header="Inline variable declaration"
145
-
Here the "counter" variable `i` is declared right in the loop. That's called an "inline" variable declaration. Such variable is visible only inside the loop.
145
+
Here the "counter" variable `i` is declared right in the loop. That's called an "inline" variable declaration. Such variables are visible only inside the loop.
146
146
147
147
```js run
148
148
for (*!*let*/!* i = 0; i < 3; i++) {
@@ -202,15 +202,15 @@ for (;;) {
202
202
}
203
203
```
204
204
205
-
Please note that the two `for` semicolons `;` must present, otherwise it would be a syntax error.
205
+
Please note that the two `for` semicolons `;` must be present, otherwise it would be a syntax error.
206
206
207
207
## Breaking the loop
208
208
209
209
Normally the loop exits when the condition becomes falsy.
210
210
211
211
But we can force the exit at any moment. There's a special `break` directive for that.
212
212
213
-
For example, this code below asks user for numbers and breaks if no number entered:
213
+
For example, the loop below asks the user for a series of numbers, but "breaks" when no number is entered:
214
214
215
215
```js
216
216
let sum =0;
@@ -353,7 +353,7 @@ In the code above `break outer` looks upwards for the label named `outer` and br
353
353
354
354
So the control goes straight from `(*)` to `alert('Done!')`.
355
355
356
-
We can also move a label into the separate string:
Copy file name to clipboardExpand all lines: 1-js/03-code-quality/02-coding-style/article.md
+20-9Lines changed: 20 additions & 9 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -46,7 +46,17 @@ Nothing is "carved in stone" here. Everything is optional and can be changed: th
46
46
47
47
In most JavaScript projects figure brackets are written on the same line, not on the new line. A so-called "egyptian" style. There's also a space before an opening bracket.
48
48
49
-
An edge case is a single-line `if/for`. Should we use brackets at all? If yes, then where?
49
+
Like this:
50
+
51
+
```js
52
+
if (condition) {
53
+
// do this
54
+
// ...and that
55
+
// ...and that
56
+
}
57
+
```
58
+
59
+
A single-line construct is an important edge case. Should we use brackets at all? If yes, then where?
50
60
51
61
Here are the annotated variants, so you can judge about their readability on your own:
52
62
@@ -82,9 +92,9 @@ There are two types of indents:
82
92
83
93
-**A horizontal indent: 2(4) spaces.**
84
94
85
-
A horizantal identation is made using either 2 or 4 spaces or the "Tab" symbol. Which one to choose is a kind of an old holy war. Spaces are more common nowadays.
95
+
A horizantal identation is made using either 2 or 4 spaces or the "Tab" symbol. Which one to choose is an old holy war. Spaces are more common nowadays.
86
96
87
-
One of advantages of spaces over tabs is that they allow more flexible configurations of indents than the "Tab" symbol.
97
+
One of advantages of spaces over tabs is that spaces allow more flexible configurations of indents than the "Tab" symbol.
88
98
89
99
For instance, we can align the arguments with the opening bracket, like this:
90
100
@@ -99,7 +109,7 @@ There are two types of indents:
99
109
}
100
110
```
101
111
102
-
-**A vertical indent: empty lines for splitting the code in logical blocks.**
112
+
-**A vertical indent: empty lines for splitting code into logical blocks.**
103
113
104
114
Even a single function can often be divided in logical blocks. In the example below, the initialization of variables, the main loop and returning the result are split vertically:
105
115
@@ -121,9 +131,9 @@ There are two types of indents:
121
131
122
132
A semicolon should be present after each statement. Evenif it could be possibly be skipped.
123
133
124
-
There are languages where a semicolon is truly optional. It's rarely used there.
134
+
There are languages where a semicolon is truly optional. It's rarely used there. But in JavaScript there are few cases when a line break is sometimes not interpreted as a semicolon. That leaves a place for programming errors.
125
135
126
-
But in JavaScript there are few cases when a line break is sometimes not interpreted as a semicolon. That leaves a place for programming errors, so semicolons should be at place.
136
+
As you become more mature as a programmer, you may choose a no-semicolon style, like [StandardJS](https://standardjs.com/), but that's only when you know JavaScript well and understand possible pitfalls.
127
137
128
138
### Nesting levels
129
139
@@ -240,7 +250,7 @@ If you are writing several "helper" functions and the code to use them, then the
240
250
...
241
251
}
242
252
```
243
-
3. Mixed, a function is described where it's first used.
253
+
3. Mixed: a function is described where it's first used.
If you're a novice developer, then you could start with the cheatsheet above in the chapter, and later browse the style guides to pick up the common principles and maybe choose one.
@@ -267,7 +278,7 @@ If you're a novice developer, then you could start with the cheatsheet above in
267
278
268
279
There are tools that can check the code style automatically. They are called "linters".
269
280
270
-
The great thing about them is that style-checking also finds some bugs, like a typo in variable name or a function.
281
+
The great thing about them is that style-checking also finds some bugs, like a typo in a variable or function name.
271
282
272
283
So it's recommended to install one, even if you don't want to stick to a "code style". They help to find typos -- and that's already good enough.
273
284
@@ -314,7 +325,7 @@ Using a linter has the great side-effect. Linters catch typos. For instance, whe
314
325
315
326
For that reason even if you're not concerned about styles, using a linter is highly recommended.
316
327
317
-
Also certain IDEs support built-in linting, but not so tunable as ESLint.
328
+
Also certain IDEs support built-in linting, that also may be good, but not so tunable as ESLint.
0 commit comments