Skip to content

Commit 482ca75

Browse files
authored
Merge branch 'master' into patch-1
2 parents d3ad4c3 + 01e87b5 commit 482ca75

File tree

327 files changed

+3919
-2502
lines changed

Some content is hidden

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

327 files changed

+3919
-2502
lines changed

1-js/01-getting-started/1-intro/article.md

+4-3
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ Let's see what's so special about JavaScript, what we can achieve with it, and w
44

55
## What is JavaScript?
66

7-
*JavaScript* was initially created to *"make web pages alive"*.
7+
*JavaScript* was initially created to "make web pages alive".
88

99
The programs in this language are called *scripts*. They can be written right in a web page's HTML and run automatically as the page loads.
1010

@@ -26,7 +26,7 @@ Different engines have different "codenames". For example:
2626

2727
- [V8](https://en.wikipedia.org/wiki/V8_(JavaScript_engine)) -- in Chrome and Opera.
2828
- [SpiderMonkey](https://en.wikipedia.org/wiki/SpiderMonkey) -- in Firefox.
29-
- ...There are other codenames like "Trident" and "Chakra" for different versions of IE, "ChakraCore" for Microsoft Edge, "Nitro" and "SquirrelFish" for Safari, etc.
29+
- ...There are other codenames like "Chakra" for IE, "ChakraCore" for Microsoft Edge, "Nitro" and "SquirrelFish" for Safari, etc.
3030

3131
The terms above are good to remember because they are used in developer articles on the internet. We'll use them too. For instance, if "a feature X is supported by V8", then it probably works in Chrome and Opera.
3232

@@ -63,7 +63,7 @@ JavaScript's abilities in the browser are limited for the sake of the user's saf
6363

6464
Examples of such restrictions include:
6565

66-
- JavaScript on a webpage may not read/write arbitrary files on the hard disk, copy them or execute programs. It has no direct access to OS system functions.
66+
- JavaScript on a webpage may not read/write arbitrary files on the hard disk, copy them or execute programs. It has no direct access to OS functions.
6767

6868
Modern browsers allow it to work with files, but the access is limited and only provided if the user does certain actions, like "dropping" a file into a browser window or selecting it via an `<input>` tag.
6969

@@ -110,6 +110,7 @@ Examples of such languages:
110110
- [TypeScript](http://www.typescriptlang.org/) is concentrated on adding "strict data typing" to simplify the development and support of complex systems. It is developed by Microsoft.
111111
- [Flow](http://flow.org/) also adds data typing, but in a different way. Developed by Facebook.
112112
- [Dart](https://www.dartlang.org/) is a standalone language that has its own engine that runs in non-browser environments (like mobile apps), but also can be transpiled to JavaScript. Developed by Google.
113+
- [Brython](https://brython.info/) is a Python transpiler to JavaScript that allow to write application in pure Python without JavaScript.
113114

114115
There are more. Of course, even if we use one of transpiled languages, we should also know JavaScript to really understand what we're doing.
115116

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<!DOCTYPE html>
2+
<html>
3+
4+
<body>
5+
6+
<script>
7+
alert( "I'm JavaScript!" );
8+
</script>
9+
10+
</body>
11+
12+
</html>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
2+
[html src="index.html"]

1-js/02-first-steps/01-hello-world/article.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ The `<script>` tag contains JavaScript code which is automatically executed when
4646
The `<script>` tag has a few attributes that are rarely used nowadays but can still be found in old code:
4747

4848
The `type` attribute: <code>&lt;script <u>type</u>=...&gt;</code>
49-
: The old HTML standard, HTML4, required a script to have a `type`. Usually it was `type="text/javascript"`. It's not required anymore. Also, the modern HTML standard totally changed the meaning of this attribute. Now, it can be used for JavaScript modules. But that's an advanced topic; we'll talk about modules in another part of the tutorial.
49+
: The old HTML standard, HTML4, required a script to have a `type`. Usually it was `type="text/javascript"`. It's not required anymore. Also, the modern HTML standard totally changed the meaning of this attribute. Now, it can be used for JavaScript modules. But that's an advanced topic, we'll talk about modules in another part of the tutorial.
5050

5151
The `language` attribute: <code>&lt;script <u>language</u>=...&gt;</code>
5252
: This attribute was meant to show the language of the script. This attribute no longer makes sense because JavaScript is the default language. There is no need to use it.
@@ -78,7 +78,7 @@ Here, `/path/to/script.js` is an absolute path to the script from the site root.
7878
We can give a full URL as well. For instance:
7979

8080
```html
81-
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/3.2.0/lodash.js"></script>
81+
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.js"></script>
8282
```
8383

8484
To attach several scripts, use multiple tags:

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ But it should be two separate statements, not one. Such a merging in this case i
9494

9595
We recommend putting semicolons between statements even if they are separated by newlines. This rule is widely adopted by the community. Let's note once again -- *it is possible* to leave out semicolons most of the time. But it's safer -- especially for a beginner -- to use them.
9696

97-
## Comments
97+
## Comments [#code-comments]
9898

9999
As time goes on, programs become more and more complex. It becomes necessary to add *comments* which describe what the code does and why.
100100

@@ -136,7 +136,7 @@ alert('World');
136136
```
137137

138138
```smart header="Use hotkeys!"
139-
In most editors, a line of code can be commented out by pressing the `key:Ctrl+/` hotkey for a single-line comment and something like `key:Ctrl+Shift+/` -- for multiline comments (select a piece of code and press the hotkey). For Mac, try `key:Cmd` instead of `key:Ctrl`.
139+
In most editors, a line of code can be commented out by pressing the `key:Ctrl+/` hotkey for a single-line comment and something like `key:Ctrl+Shift+/` -- for multiline comments (select a piece of code and press the hotkey). For Mac, try `key:Cmd` instead of `key:Ctrl` and `key:Option` instead of `key:Shift`.
140140
```
141141

142142
````warn header="Nested comments are not supported!"

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

+18-14
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,7 @@ For example:
1919
...
2020
```
2121

22-
We will learn functions (a way to group commands) soon. Looking ahead, let's note that `"use strict"` can be put at the beginning of the function body instead of the whole script. Doing that enables strict mode in that function only. But usually, people use it for the whole script.
23-
22+
Quite soon we're going to learn functions (a way to group commands), so let's note in advance that `"use strict"` can be put at the beginning of a function. Doing that enables strict mode in that function only. But usually people use it for the whole script.
2423

2524
````warn header="Ensure that \"use strict\" is at the top"
2625
Please make sure that `"use strict"` is at the top of your scripts, otherwise strict mode may not be enabled.
@@ -47,11 +46,13 @@ Once we enter strict mode, there's no going back.
4746
4847
## Browser console
4948
50-
For the future, when you use a browser console to test features, please note that it doesn't `use strict` by default.
49+
When you use a [developer console](info:devtools) to run code, please note that it doesn't `use strict` by default.
5150
5251
Sometimes, when `use strict` makes a difference, you'll get incorrect results.
5352
54-
You can try to press `key:Shift+Enter` to input multiple lines, and put `use strict` on top, like this:
53+
So, how to actually `use strict` in the console?
54+
55+
First, you can try to press `key:Shift+Enter` to input multiple lines, and put `use strict` on top, like this:
5556
5657
```js
5758
'use strict'; <Shift+Enter for a newline>
@@ -61,25 +62,28 @@ You can try to press `key:Shift+Enter` to input multiple lines, and put `use str
6162

6263
It works in most browsers, namely Firefox and Chrome.
6364

64-
If it doesn't, the most reliable way to ensure `use strict` would be to input the code into console like this:
65+
If it doesn't, e.g. in an old browser, there's an ugly, but reliable way to ensure `use strict`. Put it inside this kind of wrapper:
6566

6667
```js
6768
(function() {
6869
'use strict';
6970

70-
// ...your code...
71+
// ...your code here...
7172
})()
7273
```
7374

74-
## Always "use strict"
75+
## Should we "use strict"?
76+
77+
The question may sound obvious, but it's not so.
78+
79+
One could recommend to start scripts with `"use strict"`... But you know what's cool?
80+
81+
Modern JavaScript supports "classes" and "modules" - advanced language structures (we'll surely get to them), that enable `use strict` automatically. So we don't need to add the `"use strict"` directive, if we use them.
7582

76-
We have yet to cover the differences between strict mode and the "default" mode.
83+
**So, for now `"use strict";` is a welcome guest at the top of your scripts. Later, when your code is all in classes and modules, you may omit it.**
7784

78-
In the next chapters, as we learn language features, we'll note the differences between the strict and default modes. Luckily, there aren't many and they actually make our lives better.
85+
As of now, we've got to know about `use strict` in general.
7986

80-
For now, it's enough to know about it in general:
87+
In the next chapters, as we learn language features, we'll see the differences between the strict and old modes. Luckily, there aren't many and they actually make our lives better.
8188

82-
1. The `"use strict"` directive switches the engine to the "modern" mode, changing the behavior of some built-in features. We'll see the details later in the tutorial.
83-
2. Strict mode is enabled by placing `"use strict"` at the top of a script or function. Several language features, like "classes" and "modules", enable strict mode automatically.
84-
3. Strict mode is supported by all modern browsers.
85-
4. We recommended always starting scripts with `"use strict"`. All examples in this tutorial assume strict mode unless (very rarely) specified otherwise.
89+
All examples in this tutorial assume strict mode unless (very rarely) specified otherwise.

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

+15-2
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,6 @@ let user = 'John'
8080

8181
Technically, all these variants do the same thing. So, it's a matter of personal taste and aesthetics.
8282

83-
8483
````smart header="`var` instead of `let`"
8584
In older scripts, you may also find another keyword: `var` instead of `let`:
8685

@@ -135,6 +134,20 @@ alert(hello); // Hello world!
135134
alert(message); // Hello world!
136135
```
137136
137+
````warn header="Declaring twice triggers an error"
138+
A variable should be declared only once.
139+
140+
A repeated declaration of the same variable is an error:
141+
142+
```js run
143+
let message = "This";
144+
145+
// repeated 'let' leads to an error
146+
let message = "That"; // SyntaxError: 'message' has already been declared
147+
```
148+
So, we should declare a variable once and then refer to it without `let`.
149+
````
150+
138151
```smart header="Functional languages"
139152
It's interesting to note that there exist [functional](https://en.wikipedia.org/wiki/Functional_programming) programming languages, like [Scala](http://www.scala-lang.org/) or [Erlang](http://www.erlang.org/) that forbid changing variable values.
140153
@@ -190,7 +203,7 @@ let имя = '...';
190203
let 我 = '...';
191204
```
192205
193-
Technically, there is no error here, such names are allowed, but there is an international tradition to use English in variable names. Even if we're writing a small script, it may have a long life ahead. People from other countries may need to read it some time.
206+
Technically, there is no error here. Such names are allowed, but there is an international convention to use English in variable names. Even if we're writing a small script, it may have a long life ahead. People from other countries may need to read it some time.
194207
````
195208

196209
````warn header="Reserved names"

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

+31-24
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
11
# Data types
22

3-
A variable in JavaScript can contain any data. A variable can at one moment be a string and at another be a number:
3+
A value in JavaScript is always of a certain type. For example, a string or a number.
4+
5+
There are eight basic data types in JavaScript. Here, we'll cover them in general and in the next chapters we'll talk about each of them in detail.
6+
7+
We can put any type in a variable. For example, a variable can at one moment be a string and then store a number:
48

59
```js
610
// no error
711
let message = "hello";
812
message = 123456;
913
```
1014

11-
Programming languages that allow such things are called "dynamically typed", meaning that there are data types, but variables are not bound to any of them.
12-
13-
There are eight basic data types in JavaScript. Here, we'll cover them in general and in the next chapters we'll talk about each of them in detail.
15+
Programming languages that allow such things, such as JavaScript, are called "dynamically typed", meaning that there exist data types, but variables are not bound to any of them.
1416

1517
## Number
1618

@@ -64,21 +66,23 @@ We'll see more about working with numbers in the chapter <info:number>.
6466

6567
## BigInt
6668

67-
In JavaScript, the "number" type cannot represent integer values larger than <code>2<sup>53</sup></code> (or less than <code>-2<sup>53</sup></code> for negatives), that's a technical limitation caused by their internal representation. That's about 16 decimal digits, so for most purposes the limitation isn't a problem, but sometimes we need really big numbers, e.g. for cryptography or microsecond-precision timestamps.
69+
In JavaScript, the "number" type cannot represent integer values larger than <code>(2<sup>53</sup>-1)</code> (that's `9007199254740991`), or less than <code>-(2<sup>53</sup>-1)</code> for negatives. It's a technical limitation caused by their internal representation.
70+
71+
For most purposes that's quite enough, but sometimes we need really big numbers, e.g. for cryptography or microsecond-precision timestamps.
6872

6973
`BigInt` type was recently added to the language to represent integers of arbitrary length.
7074

71-
A `BigInt` is created by appending `n` to the end of an integer literal:
75+
A `BigInt` value is created by appending `n` to the end of an integer:
7276

7377
```js
7478
// the "n" at the end means it's a BigInt
7579
const bigInt = 1234567890123456789012345678901234567890n;
7680
```
7781

78-
As `BigInt` numbers are rarely needed, we devoted them a separate chapter <info:bigint>.
82+
As `BigInt` numbers are rarely needed, we don't cover them here, but devoted them a separate chapter <info:bigint>. Read it when you need such big numbers.
7983

80-
```smart header="Compatability issues"
81-
Right now `BigInt` is supported in Firefox and Chrome, but not in Safari/IE/Edge.
84+
```smart header="Compatibility issues"
85+
Right now `BigInt` is supported in Firefox/Chrome/Edge, but not in Safari/IE.
8286
```
8387

8488
## String
@@ -123,7 +127,7 @@ We'll cover strings more thoroughly in the chapter <info:string>.
123127
```smart header="There is no *character* type."
124128
In some languages, there is a special "character" type for a single character. For example, in the C language and in Java it is called "char".
125129
126-
In JavaScript, there is no such type. There's only one type: `string`. A string may consist of only one character or many of them.
130+
In JavaScript, there is no such type. There's only one type: `string`. A string may consist of zero characters (be empty), one character or many of them.
127131
```
128132

129133
## Boolean (logical type)
@@ -163,7 +167,7 @@ In JavaScript, `null` is not a "reference to a non-existing object" or a "null p
163167

164168
It's just a special value which represents "nothing", "empty" or "value unknown".
165169

166-
The code above states that `age` is unknown or empty for some reason.
170+
The code above states that `age` is unknown.
167171

168172
## The "undefined" value
169173

@@ -174,30 +178,33 @@ The meaning of `undefined` is "value is not assigned".
174178
If a variable is declared, but not assigned, then its value is `undefined`:
175179

176180
```js run
177-
let x;
181+
let age;
178182

179-
alert(x); // shows "undefined"
183+
alert(age); // shows "undefined"
180184
```
181185

182-
Technically, it is possible to assign `undefined` to any variable:
186+
Technically, it is possible to explicitly assign `undefined` to a variable:
183187

184188
```js run
185-
let x = 123;
189+
let age = 100;
186190

187-
x = undefined;
191+
// change the value to undefined
192+
age = undefined;
188193

189-
alert(x); // "undefined"
194+
alert(age); // "undefined"
190195
```
191196

192-
...But we don't recommend doing that. Normally, we use `null` to assign an "empty" or "unknown" value to a variable, and we use `undefined` for checks like seeing if a variable has been assigned.
197+
...But we don't recommend doing that. Normally, one uses `null` to assign an "empty" or "unknown" value to a variable, while `undefined` is reserved as a default initial value for unassigned things.
193198

194199
## Objects and Symbols
195200

196201
The `object` type is special.
197202

198-
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 of data and more complex entities. We'll deal with them later in the chapter <info:object> after we learn more about primitives.
203+
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 of data and more complex entities.
204+
205+
Being that important, objects deserve a special treatment. We'll deal with them later in the chapter <info:object>, after we learn more about primitives.
199206

200-
The `symbol` type is used to create unique identifiers for objects. We mention it here for completeness, but we'll study it after objects.
207+
The `symbol` type is used to create unique identifiers for objects. We have to mention it here for the sake of completeness, but also postpone the details till we know objects.
201208

202209
## The typeof operator [#type-typeof]
203210

@@ -241,16 +248,16 @@ typeof alert // "function" (3)
241248
The last three lines may need additional explanation:
242249

243250
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.
244-
2. The result of `typeof null` is `"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, this is an error in the language.
245-
3. The result of `typeof alert` is `"function"`, because `alert` is a function. We'll study functions in the next chapters where we'll also see that there's no special "function" type in JavaScript. Functions belong to the object type. But `typeof` treats them differently, returning `"function"`. That's not quite correct, but very convenient in practice.
251+
2. The result of `typeof null` is `"object"`. That's an officially recognized error in `typeof` behavior, coming from the early days of JavaScript and kept for compatibility. Definitely, `null` is not an object. It is a special value with a separate type of its own.
252+
3. The result of `typeof alert` is `"function"`, because `alert` is a function. We'll study functions in the next chapters where we'll also see that there's no special "function" type in JavaScript. Functions belong to the object type. But `typeof` treats them differently, returning `"function"`. That also comes from the early days of JavaScript. Technically, such behavior isn't correct, but can be convenient in practice.
246253

247254
## Summary
248255

249256
There are 8 basic data types in JavaScript.
250257

251-
- `number` for numbers of any kind: integer or floating-point, integers are limited by ±2<sup>53</sup>.
258+
- `number` for numbers of any kind: integer or floating-point, integers are limited by <code>±(2<sup>53</sup>-1)</code>.
252259
- `bigint` is for integer numbers of arbitrary length.
253-
- `string` for strings. A string may have one or more characters, there's no separate single-character type.
260+
- `string` for strings. A string may have zero or more characters, there's no separate single-character type.
254261
- `boolean` for `true`/`false`.
255262
- `null` for unknown values -- a standalone type that has a single value `null`.
256263
- `undefined` for unassigned values -- a standalone type that has a single value `undefined`.

0 commit comments

Comments
 (0)