Skip to content

Commit 6598e0c

Browse files
committed
Merge branch 'master' of github.com:javascript-tutorial/en.javascript.info into sync-fb4fc33a
2 parents 78bfaa9 + fb4fc33 commit 6598e0c

48 files changed

Lines changed: 232 additions & 172 deletions

File tree

Some content is hidden

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

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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 "Chakra" for IE, "ChakraCore" for Microsoft Edge, "Nitro" and "SquirrelFish" for Safari, etc.
29+
- ...There are other codenames like "Chakra" for IE, "JavaScriptCore", "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

1-js/01-getting-started/4-devtools/article.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ To see errors and get a lot of other useful information about scripts, "develope
88

99
Most developers lean towards Chrome or Firefox for development because those browsers have the best developer tools. Other browsers also provide developer tools, sometimes with special features, but are usually playing "catch-up" to Chrome or Firefox. So most developers have a "favorite" browser and switch to others if a problem is browser-specific.
1010

11-
Developer tools are potent, they have many features. To start, we'll learn how to open them, look at errors, and run JavaScript commands.
11+
Developer tools are potent; they have many features. To start, we'll learn how to open them, look at errors, and run JavaScript commands.
1212

1313
## Google Chrome
1414

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

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ alert(3 +
4646
+ 2);
4747
```
4848

49-
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", so the semicolon is not required. And in this case that works as intended.
49+
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", so a semicolon there would be incorrect. And in this case, that works as intended.
5050

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

@@ -56,40 +56,36 @@ Errors which occur in such cases are quite hard to find and fix.
5656
If you're curious to see a concrete example of such an error, check this code out:
5757
5858
```js run
59-
[1, 2].forEach(alert)
59+
alert("Hello");
60+
61+
[1, 2].forEach(alert);
6062
```
6163
62-
No need to think about the meaning of the brackets `[]` and `forEach` yet. We'll study them later. For now, just remember the result of the code: it shows `1` then `2`.
64+
No need to think about the meaning of the brackets `[]` and `forEach` yet. We'll study them later. For now, just remember the result of running the code: it shows `Hello`, then `1`, then `2`.
6365
64-
Now, let's add an `alert` before the code and *not* finish it with a semicolon:
66+
Now let's remove the semicolon after the `alert`:
6567
6668
```js run no-beautify
67-
alert("There will be an error")
69+
alert("Hello")
6870
69-
[1, 2].forEach(alert)
71+
[1, 2].forEach(alert);
7072
```
7173
72-
Now if we run the code, only the first `alert` is shown and then we have an error!
73-
74-
But everything is fine again if we add a semicolon after `alert`:
75-
```js run
76-
alert("All fine now");
74+
The difference compared to the code above is only one character: the semicolon at the end of the first line is gone.
7775
78-
[1, 2].forEach(alert)
79-
```
76+
If we run this code, only the first `Hello` shows (and there's an error, you may need to open the console to see it). There are no numbers any more.
8077
81-
Now we have the "All fine now" message followed by `1` and `2`.
78+
That's because JavaScript does not assume a semicolon before square brackets `[...]`. So, the code in the last example is treated as a single statement.
8279
83-
84-
The error in the no-semicolon variant occurs because JavaScript does not assume a semicolon before square brackets `[...]`.
85-
86-
So, because the semicolon is not auto-inserted, the code in the first example is treated as a single statement. Here's how the engine sees it:
80+
Here's how the engine sees it:
8781
8882
```js run no-beautify
89-
alert("There will be an error")[1, 2].forEach(alert)
83+
alert("Hello")[1, 2].forEach(alert);
9084
```
9185
92-
But it should be two separate statements, not one. Such a merging in this case is just wrong, hence the error. This can happen in other situations.
86+
Looks weird, right? Such merging in this case is just wrong. We need to put a semicolon after `alert` for the code to work correctly.
87+
88+
This can happen in other situations also.
9389
````
9490

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

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ Special numeric values formally belong to the "number" type. Of course they are
6464

6565
We'll see more about working with numbers in the chapter <info:number>.
6666

67-
## BigInt
67+
## BigInt [#bigint-type]
6868

6969
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.
7070

1-js/02-first-steps/08-operators/article.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,8 @@ In school maths, we write that as a<sup>b</sup>.
6363
For instance:
6464

6565
```js run
66-
alert( 2 ** 2 ); // 2² = 4
67-
alert( 2 ** 3 ); // 2³ = 8
66+
alert( 2 ** 2 ); // 2² = 4
67+
alert( 2 ** 3 ); // 2³ = 8
6868
alert( 2 ** 4 ); // 2⁴ = 16
6969
```
7070

1-js/02-first-steps/15-function-basics/article.md

Lines changed: 34 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@ function showMessage() {
2020
}
2121
```
2222

23-
The `function` keyword goes first, then goes the *name of the function*, then a list of *parameters* between the parentheses (comma-separated, empty in the example above) and finally the code of the function, also named "the function body", between curly braces.
23+
The `function` keyword goes first, then goes the *name of the function*, then a list of *parameters* between the parentheses (comma-separated, empty in the example above, we'll see examples later) and finally the code of the function, also named "the function body", between curly braces.
2424

2525
```js
26-
function name(parameters) {
26+
function name(parameter1, parameter2, ... parameterN) {
2727
...body...
2828
}
2929
```
@@ -137,26 +137,23 @@ It's a good practice to minimize the use of global variables. Modern code has fe
137137

138138
## Parameters
139139

140-
We can pass arbitrary data to functions using parameters (also called *function arguments*) .
140+
We can pass arbitrary data to functions using parameters.
141141

142142
In the example below, the function has two parameters: `from` and `text`.
143143

144144
```js run
145-
function showMessage(*!*from, text*/!*) { // arguments: from, text
145+
function showMessage(*!*from, text*/!*) { // parameters: from, text
146146
alert(from + ': ' + text);
147147
}
148148

149-
*!*
150-
showMessage('Ann', 'Hello!'); // Ann: Hello! (*)
151-
showMessage('Ann', "What's up?"); // Ann: What's up? (**)
152-
*/!*
149+
*!*showMessage('Ann', 'Hello!');*/!* // Ann: Hello! (*)
150+
*!*showMessage('Ann', "What's up?");*/!* // Ann: What's up? (**)
153151
```
154152
155153
When the function is called in lines `(*)` and `(**)`, the given values are copied to local variables `from` and `text`. Then the function uses them.
156154
157155
Here's one more example: we have a variable `from` and pass it to the function. Please note: the function changes `from`, but the change is not seen outside, because a function always gets a copy of the value:
158156

159-
160157
```js run
161158
function showMessage(from, text) {
162159

@@ -175,19 +172,31 @@ showMessage(from, "Hello"); // *Ann*: Hello
175172
alert( from ); // Ann
176173
```
177174
175+
When a value is passed as a function parameter, it's also called an *argument*.
176+
177+
In other words, to put these terms straight:
178+
179+
- A parameter is the variable listed inside the parentheses in the function declaration (it's a declaration time term)
180+
- An argument is the value that is passed to the function when it is called (it's a call time term).
181+
182+
We declare functions listing their parameters, then call them passing arguments.
183+
184+
In the example above, one might say: "the function `sayMessage` is declared with two parameters, then called with two arguments: `from` and `"Hello"`".
185+
186+
178187
## Default values
179188
180-
If a parameter is not provided, then its value becomes `undefined`.
189+
If a function is called, but an argument is not provided, then the corresponding value becomes `undefined`.
181190
182191
For instance, the aforementioned function `showMessage(from, text)` can be called with a single argument:
183192
184193
```js
185194
showMessage("Ann");
186195
```
187196
188-
That's not an error. Such a call would output `"*Ann*: undefined"`. There's no `text`, so it's assumed that `text === undefined`.
197+
That's not an error. Such a call would output `"*Ann*: undefined"`. As the value for `text` isn't passed, it becomes `undefined`.
189198
190-
If we want to use a "default" `text` in this case, then we can specify it after `=`:
199+
We can specify the so-called "default" (to use if omitted) value for a parameter in the function declaration, using `=`:
191200
192201
```js run
193202
function showMessage(from, *!*text = "no text given"*/!*) {
@@ -211,19 +220,23 @@ function showMessage(from, text = anotherFunction()) {
211220
```smart header="Evaluation of default parameters"
212221
In JavaScript, a default parameter is evaluated every time the function is called without the respective parameter.
213222

214-
In the example above, `anotherFunction()` is called every time `showMessage()` is called without the `text` parameter.
223+
In the example above, `anotherFunction()` isn't called at all, if the `text` parameter is provided.
224+
225+
On the other hand, it's independently called every time when `text` is missing.
215226
```
216227

217228
### Alternative default parameters
218229

219-
Sometimes it makes sense to set default values for parameters not in the function declaration, but at a later stage, during its execution.
230+
Sometimes it makes sense to assign default values for parameters not in the function declaration, but at a later stage.
220231

221-
To check for an omitted parameter, we can compare it with `undefined`:
232+
We can check if the parameter is passed during the function execution, by comparing it with `undefined`:
222233

223234
```js run
224235
function showMessage(text) {
236+
// ...
237+
225238
*!*
226-
if (text === undefined) {
239+
if (text === undefined) { // if the parameter is missing
227240
text = 'empty message';
228241
}
229242
*/!*
@@ -234,21 +247,21 @@ function showMessage(text) {
234247
showMessage(); // empty message
235248
```
236249
237-
...Or we could use the `||` operator:
250+
...Or we could use the `??` operator:
238251
239252
```js
240-
// if text parameter is omitted or "" is passed, set it to 'empty'
241253
function showMessage(text) {
254+
// if text is undefined or otherwise falsy, set it to 'empty'
242255
text = text || 'empty';
243256
...
244257
}
245258
```
246259
247-
Modern JavaScript engines support the [nullish coalescing operator](info:nullish-coalescing-operator) `??`, it's better when falsy values, such as `0`, are considered regular:
260+
Modern JavaScript engines support the [nullish coalescing operator](info:nullish-coalescing-operator) `??`, it's better when most falsy values, such as `0`, should be considered "normal":
248261
249262
```js run
250-
// if there's no "count" parameter, show "unknown"
251263
function showCount(count) {
264+
// if count is undefined or null, show "unknown"
252265
alert(count ?? "unknown");
253266
}
254267

@@ -411,7 +424,7 @@ Functions that are used *very often* sometimes have ultrashort names.
411424

412425
For example, the [jQuery](http://jquery.com) framework defines a function with `$`. The [Lodash](http://lodash.com/) library has its core function named `_`.
413426

414-
These are exceptions. Generally functions names should be concise and descriptive.
427+
These are exceptions. Generally function names should be concise and descriptive.
415428
```
416429

417430
## Functions == Comments

1-js/03-code-quality/01-debugging-chrome/article.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Debugging in Chrome
1+
# Debugging in the browser
22

33
Before writing more complex code, let's talk about debugging.
44

1-js/03-code-quality/05-testing-mocha/article.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
Automated testing will be used in further tasks, and it's also widely used in real projects.
44

5-
## Why we need tests?
5+
## Why do we need tests?
66

77
When we write a function, we can usually imagine what it should do: which parameters give which results.
88

1-js/03-code-quality/06-polyfills/article.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ Modern project build systems, such as [webpack](http://webpack.github.io/), prov
4848
4949
New language features may include not only syntax constructs and operators, but also built-in functions.
5050
51-
For example, `Math.trunc(n)` is a function that "cuts off" the decimal part of a number, e.g `Math.trunc(1.23) = 1`.
51+
For example, `Math.trunc(n)` is a function that "cuts off" the decimal part of a number, e.g `Math.trunc(1.23)` returns `1`.
5252
5353
In some (very outdated) JavaScript engines, there's no `Math.trunc`, so such code will fail.
5454

1-js/04-object-basics/04-object-methods/article.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ user = {
8181
// method shorthand looks better, right?
8282
user = {
8383
*!*
84-
sayHi() { // same as "sayHi: function()"
84+
sayHi() { // same as "sayHi: function(){...}"
8585
*/!*
8686
alert("Hello");
8787
}

0 commit comments

Comments
 (0)