Skip to content

Commit 5372c18

Browse files
committed
merges
1 parent 7796019 commit 5372c18

File tree

152 files changed

+480
-369
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

152 files changed

+480
-369
lines changed

1-js/2-first-steps/02-structure/article.md

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ The first overall thing to know is the code structure.
66

77
## Statements
88

9-
[Statements](https://en.wikipedia.org/wiki/Statement_(computer_science)) are syntax constructs and commands to perform actions.
9+
Statements are syntax constructs and commands that perform actions.
1010

1111
We've already seen a statement `alert('Hello, world!')`, which shows the message.
1212

@@ -48,11 +48,11 @@ alert(3 +
4848
+ 2);
4949
```
5050

51-
The code outputs `6`, because JavaScript does not insert semicolons here. It is intuitively obvious that if the line ends with a plus `"+"`, then it is an "incomplete expression". And in this case that's actually fine and comfortable.
51+
The code outputs `6`, because JavaScript does not insert semicolons here. It is intuitively obvious that if the line ends with a plus `"+"`, then it is an "incomplete expression", no semicolon required. And in this case that works as intended.
5252

5353
**But there are situations where JavaScript "fails" to assume a semicolon where it is really needed.**
5454

55-
Errors which come appear in such cases are quite hard to find and fix.
55+
Errors which occur in such cases are quite hard to find and fix.
5656

5757
````smart header="An example of the error"
5858
If you're curious to see a concrete example, check this code out:
@@ -65,25 +65,29 @@ It shows `1` then `2`.
6565
6666
No need to think about the meaning of the brackets `[]` and `forEach`, for now -- it does not matter. Let's just remember the result.
6767
68-
Now we prepend the code with an `alert` statement *not followed by a semicolon*:
68+
Now let's put an `alert` before the code. And *not* finish it with a semicolon:
6969
7070
```js run no-beautify
7171
alert( "There will be an error" ) // shown
72-
[1, 2].forEach(alert) // doesn't work!
72+
73+
[1, 2].forEach(alert) // doesn't work any more!
7374
```
7475
7576
Now if we run it, only the first `alert` is shown, and then an error.
7677
77-
But everything's fine if we add a semicolon:
78+
But everything is fine again if we add a semicolon after `alert`:
7879
```js run
7980
alert( "All fine now" ); // shown
81+
8082
[1, 2].forEach(alert) // this works too
8183
```
8284
83-
The error in the no-semicolon variant occurs because JavaScript engine does not assume a semicolon before square brackets `[...]`, so the code is actually treated as a one-line statement:
85+
The error in the no-semicolon variant occurs because automatic semicolon insertion rules are complex, and in particular, JavaScript does not imply a semicolon before square brackets `[...]`.
86+
87+
And, because the semicolon is not auto-inserted, the code is treated as a single statement, like this:
8488
8589
```js run no-beautify
86-
alert( "There will be an error" )[1, 2].forEach(alert)
90+
alert( "There will be an error" )[1, 2].forEach(alert)
8791
```
8892
8993
And in this particular case, that's just wrong, hence the error. There are other situations when such thing happens.
@@ -133,11 +137,11 @@ alert( 'World' );
133137
```
134138

135139
```smart header="Use hotkeys!"
136-
In most editors a line of code can be commented out by `key:Ctrl+/` hotkey for a single-line comment and something like `key:Ctrl+Shift+/` -- for multiline comments (select a code and press the hotkey).
140+
In most editors a line of code can be commented out by `key:Ctrl+/` hotkey for a single-line comment and something like `key:Ctrl+Shift+/` -- for multiline comments (select a code and press the hotkey). For Mac try `key:Cmd` instead of `key:Ctrl`.
137141
```
138142

139143
````warn header="Nested comments are not supported!"
140-
There may not be comments inside comments.
144+
There may not be `/*...*/` inside another `/*...*/`.
141145
142146
This code will die with an error:
143147
@@ -155,3 +159,4 @@ Comments increase the overall code footprint, but that's not a problem at all. T
155159

156160
Further in the tutorial we'll devote a special chapter to code style, also explaining how to write better comments.
157161

162+
[todo is there such a chapter?]

1-js/2-first-steps/03-strict-mode/article.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,9 @@ Only comments may appear above `"use strict"`.
4848
4949
5050
```smart header="`use strict` for functions"
51-
We will learn functions (a way to group commands) soon.
51+
We will learn functions (a way to group commands) soon.
5252
53-
Looking ahead let's just note that `"use strict"` can be put at the start of a function instead of the whole script. Then the strict mode is enabled in that function only. But usually people put it on top of scripts.
53+
Looking ahead let's just note that `"use strict"` can be put at the start of a function (most kinds of functions) instead of the whole script. Then strict mode is enabled in that function only. But usually people use it for the whole script.
5454
```
5555
5656
@@ -66,5 +66,6 @@ Here in the tutorial all code (except where said otherwise) works in `"use stric
6666
## Summary
6767
6868
- The `"use strict"` directive switches the engine to the "modern" mode, changing the behavior of some builtin features.
69-
- Several modern features of the language enable `"use strict"` implicitly.
70-
- It's recommended to start scripts with `"use strict"`.
69+
- Several modern features of the language enable `"use strict"` implicitly, so it's quite hard to evade it.
70+
71+
It's always recommended to start scripts with `"use strict"`. All examples in this book assume so, unless (very rarely) specified otherwise.

1-js/2-first-steps/04-variables/article.md

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ To be concise we can merge the variable declaration and assignment into a single
4646
```js run
4747
let message = 'Hello!'; // define the variable and assign the value
4848

49-
alert( message ); // Hello!
49+
alert(message); // Hello!
5050
```
5151

5252
We can also declare multiple variables in one line:
@@ -55,11 +55,11 @@ We can also declare multiple variables in one line:
5555
let user = 'John', age = 25, message = 'Hello';
5656
```
5757

58-
That might seem shorter, but it's not recommended. For the sake of beter readability, please use a single line per variable.
58+
That might seem shorter, but it's not recommended. For the sake of better readability, please use a single line per variable.
5959

60-
The rewritten code is a bit longer, but easier to read:
60+
The multiline variant is a bit longer, but easier to read:
6161

62-
```js no-beautify
62+
```js
6363
let user = 'John';
6464
let age = 25;
6565
let message = 'Hello';
@@ -80,7 +80,7 @@ let user = 'John'
8080
, message = 'Hello';
8181
```
8282

83-
Technically, all these variants do the same. So, it's a matter of personal taste and aestetics.
83+
Technically, all these variants do the same. So, it's a matter of personal taste and aesthetics.
8484

8585

8686
````smart header="`var` instead of `let`"
@@ -103,7 +103,7 @@ For instance, the variable `message` can be imagined as a box labelled `"message
103103
104104
![](variable.png)
105105
106-
We can put any value into the box.
106+
We can put any value into the box.
107107
108108
Also we can change it. The value can be changed as many times as needed:
109109
@@ -211,12 +211,12 @@ let return = 5; // also can't name it "return", error!
211211

212212
````warn header="An assignment without `use strict`"
213213

214-
Normally, we need to define a variable before using it. But in the old times, it was technically possible to create a variable by a mere assignment of the value. This still works now in if we don't put `use strict`, the behavior is kept for compatibility with old scripts.
214+
Normally, we need to define a variable before using it. But in the old times, it was technically possible to create a variable by a mere assignment of the value. This still works now in if we don't put `use strict`, the behavior is kept for compatibility with old scripts.
215215

216216
```js run no-strict
217217
num = 5; // the variable "num" is created if didn't exist
218218

219-
alert(num); // 5
219+
alert(num); // 5
220220
```
221221

222222
That's a bad practice of course, it gives an error in the strict mode:
@@ -252,7 +252,7 @@ When a programmer is sure that the variable should never change, he can use `con
252252
253253
### Uppercase constants
254254
255-
There is a widespread practice to use constants as aliases for difficult-to-remember values that are known prior to execution.
255+
There is a widespread practice to use constants as aliases for difficult-to-remember values that are known prior to execution.
256256
257257
Such constants are named using capitals and underscores.
258258
@@ -269,19 +269,29 @@ let color = COLOR_ORANGE;
269269
alert( color ); // #FF7F00
270270
```
271271
272-
`COLOR_ORANGE` is much easier to remember than `"#FF7F00"`. Also it is much easier to mistype in `"#FF7F00"` than in `COLOR_ORANGE`. And when reading the code -- `COLOR_ORANGE` is much more meaningful.
272+
`COLOR_ORANGE` is much easier to remember than `"#FF7F00"`. Also it is much easier to mistype in `"#FF7F00"` than in `COLOR_ORANGE`. And when reading the code -- `COLOR_ORANGE` is much more meaningful.
273+
274+
When should we use capitals for a constant, and when -- name them normally? Let's make that clear.
275+
276+
Being a "constant" just means that the value never changes. But there are constants that are known prior to execution (like a hexadimal value for red), and there are those that are *calculated* during the execution, but do not change since then.
277+
278+
```js
279+
const ordinaryConst = /* an expression that uses the current date */;
280+
// the expression value is not known prior to execution
281+
// such a constant must be named normally
282+
```
283+
284+
In other words, capital-named constants are only used as aliases for "hard-coded" values.
273285
274286
## Name things right
275287
276-
Talking about variables, there's an exteremely important thing.
288+
Talking about variables, there's one more exteremely important thing.
277289
278290
Please name the variables sensibly.
279291
280292
Variable naming is one of the most important and complex skills in programming. A quick glance at variable names can obviously show which code is written by a beginner and which by an experienced guru.
281293
282-
In a real project, most of the time is spent on modifying and extending the existing code, rather than writing something completely new.
283-
284-
And when we return to the code after some time of doing something else, it's much easier to find the information that is well-labelled. Or, in other words, when the variables are named right.
294+
In a real project, most of the time is spent on modifying and extending the existing code base, rather than writing something completely separate of it. And when we return to the code after some time of doing something else, it's much easier to find the information that is well-labelled. Or, in other words, when the variables have good names.
285295
286296
Please spend some time thinking about the right name for a variable before declaring it. That will repay you a lot.
287297
@@ -290,7 +300,7 @@ Few good-to-follow rules are:
290300
- Use human-readable names like `userName` or `shoppingCart`.
291301
- Stay away from abbreviations or short names `a`, `b`, `c`, unless you really know what you're doing.
292302
- Make the name maximally descriptive and concise. Examples of bad names are `data` and `value`. Such a name says nothing. It is only ok to use them if it's exceptionally obvious from the context which data or value is meant.
293-
- Agree on terms within the team and in your own mind. If a site visitor is called a "user" then we should name variables like `currentUser` or `newUser`, but not `currentVisitor` or a `newManInTown`.
303+
- Agree on terms within the team and in your own mind. If a site visitor is called a "user" then we should name related variables like `currentUser` or `newUser`, but not `currentVisitor` or a `newManInTown`.
294304
295305
Sounds simple? Indeed it is. But creating good descriptive-and-concise names in practice is not. Go for it.
296306
@@ -301,7 +311,7 @@ As the result, the variable is like a box where people throw different things wi
301311
302312
Such a programmer saves a little bit on variable declaration, but looses ten times more on debugging the code.
303313
304-
An extra variable is good, not evil.
314+
An extra variable is good, not evil.
305315
306316
Modern JavaScript minifiers and browsers optimize code well enough, so it won't create performance issues. Using different variables for values of different types can even help the engine to optimize better.
307317
```
@@ -314,4 +324,4 @@ We can declare variables to store data. That can be done using `var` or `let` or
314324
- `var` -- is an old-school variable declaration. We'll study the subtle differences from `let` later.
315325
- `const` -- is like `let`, but the variable can't be changed.
316326
317-
Variables should be named in a way that allows to easily understand what's inside.
327+
Variables should be named in a way that allows to easily understand what's inside.

1-js/2-first-steps/05-types/article.md

Lines changed: 27 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,16 @@
11
# Data types
22

3-
There are 7 data types in JavaScript.
3+
A variable in JavaScript can contain any data. A variable can at one moment be a string and later recieve a numeric value:
44

5-
In this chapter we'll get the common understanding of them. In further chapters we'll talk about each type in detail.
5+
```js
6+
// no error
7+
let message = "hello";
8+
message = 123456;
9+
```
10+
11+
Such languages are called "dynamically typed", meaning that there are language types, but variables are not bound to any of them.
12+
13+
There are 7 basic data types in JavaScript. Here we'll study the basics, and in the next chapters we'll talk about each of them in detail.
614

715
[cut]
816

@@ -19,7 +27,7 @@ There are many operations for numbers, e.g. multiplication `*`, division `/`, ad
1927

2028
Besides regular numbers there are so-called "special numeric values" which also belong to that type: `Infinity`, `-Infinity` and `NaN`.
2129

22-
- `Infinity` represents the mathematical [Infinity](https://en.wikipedia.org/wiki/Infinity). It is a value that's greater than any number.
30+
- `Infinity` represents the mathematical [Infinity](https://en.wikipedia.org/wiki/Infinity). It is a special value that's greater than any number.
2331

2432
We can get it as a result of division by zero:
2533

@@ -44,12 +52,12 @@ Besides regular numbers there are so-called "special numeric values" which also
4452
alert( "not a number" / 2 + 5 ); // NaN + 5 is still NaN
4553
```
4654

47-
So, in a long mathematical expression if we have `NaN` in one place, it propagates to the whole result.
55+
So, if there's `NaN` somewhere in a mathematical expression, it propagates to the whole result.
4856
4957
```smart header="Mathematical operations are safe"
50-
Doing maths is safe in JavaScript. We can do anything: divide by zero, treat non-numeric strings as numbers, etc.
58+
Doing maths is safe in JavaScript. We can do anything: divide by zero, treat non-numeric strings as numbers, etc.
5159
52-
The script will never stop with a fatal error ("die"). At worst we'll get `NaN` as the result.
60+
The script will never stop with a fatal error ("die"). At worst we'll get `NaN` as the result.
5361
```
5462

5563
Special numeric values formally belong to the "number" type. Of course they are not numbers in a common sense of this word.
@@ -80,7 +88,7 @@ let name = "John";
8088
// embed variable
8189
alert( `Hello, ${name}!` ); // Hello, John!
8290

83-
// embed expression
91+
// embed expression
8492
alert( `the result is ${1 + 2}` ); // the result is 3
8593
```
8694

@@ -98,24 +106,24 @@ In JavaScript, there is no such type. There's only one type: `string`. A string
98106

99107
The boolean type has only two values: `true` and `false`.
100108

101-
This type is commonly used to store yes/no values.
109+
This type is commonly used to store yes/no values: `true` means "yes, correct", and `false` means the "no, incorrect".
102110

103111
For instance:
104112

105-
```js no-beautify
106-
let checked = true; // the form field is checked
107-
checked = false; // the form field is not checked
113+
```js
114+
let checked1 = true; // yes, the form field is checked
115+
let checked2 = false; // no, the form field is not checked
108116
```
109117

110118
Boolean values also come as the result of comparisons:
111119

112120
```js run
113-
let isGreater = 4 > 1;
121+
let isGreater = 4 > 1;
114122

115123
alert( isGreater ); // true (the comparison result is "yes")
116124
```
117125

118-
We'll cover booleans more deeply while discussing logical operators.
126+
We'll cover booleans more deeply later while discussing [logical operators](/logical-ops).
119127

120128
## The "null" value
121129

@@ -131,13 +139,13 @@ In JavaScript `null` is not a "reference to a non-existing object" or a "null po
131139

132140
It's just a special value which has the sense of "nothing", "empty" or "value unknown".
133141

134-
The code above states that the `age` is unknown or empty for some reason.
142+
The code above states that the `age` is unknown or empty for some reason.
135143

136144
## The "undefined" value
137145

138146
The special value `undefined` stands apart. It makes a type of its own, just like `null`.
139147

140-
The sense of `undefined` is "value is not assigned".
148+
The sense of `undefined` is "value is not assigned".
141149

142150
If a variable is declared, but not assigned, then its value is exactly `undefined`:
143151

@@ -163,15 +171,11 @@ alert( x ); // "undefined"
163171

164172
The `object` type is special.
165173

166-
All other types are called "primitive", because their values can contain only a single thing (be it a string or a number or whatever).
167-
168-
In contrast, objects are used to store collections data and more complex entities. We'll deal with them later after we know enough about primitives.
169-
170-
The `symbol` type is used to create unique identifiers, mainly used to store data in objects. We'll return to them as soon as we cover objects.
174+
All other types are called "primitive", because their values can contain only a single thing (be it a string or a number or whatever). In contrast, objects are used to store collections data and more complex entities. We'll deal with them later after we know enough about primitives.
171175

176+
The `symbol` type is used to create unique identifiers for objects. We have to mention it here for completeness, but we'd better study them after covering objects.
172177

173178
[todo when ? chapter?]
174-
[todo move tasks]
175179

176180
## The typeof operator [#type-typeof]
177181
[todo we need typeof in types]
@@ -213,7 +217,7 @@ typeof alert // "function" (3)
213217

214218
The last three lines may be a little unobvious so here's explanations:
215219

216-
1. `Math` is a built-in object that provides mathematical operations. We will learn it in the chapter <info:number>. Here it servers as an example, nothing more.
220+
1. `Math` is a built-in object that provides mathematical operations. We will learn it in the chapter <info:number>. Here it serves just as an example of an object.
217221
2. The result of `typeof null` equals to `"object"`. That's wrong. It is an officially recognized error in `typeof`, kept for compatibility. Of course, `null` is not an object. It is a special value with a separate type of its own. So, again, that's an error in the language.
218222
3. The result of `typeof alert` is `"function"`, because `alert` is a function of the language. We'll study functions in the near future and see that actually functions belong to the object type. But `typeof` treats them differently. That's very convenient in practice.
219223

@@ -230,13 +234,10 @@ There are 7 basic types in JavaScript.
230234
- `object` for more complex data structures.
231235
- `symbol` for unique identifiers.
232236

233-
The `typeof` operator allows to see which type is stored in the variable.
237+
The `typeof` operator allows to see which type is stored in the variable.
234238

235239
- Two forms: `typeof x` or `typeof(x)`.
236240
- Returns a string with the name of the type, like `"string"`.
237241
- Mistreats `null` as an `object` -- the old official error in the language.
238242

239243
In nearest chapters we'll concentrate on understanding how to operate on primitives and once we're familiar with that, then we'll move on to objects.
240-
241-
242-

0 commit comments

Comments
 (0)