Skip to content

Commit f4ec378

Browse files
committed
Merge branch 'master' of github.com:javascript-tutorial/en.javascript.info into sync-29a3c58d
2 parents 737460e + 29a3c58 commit f4ec378

File tree

60 files changed

+263
-274
lines changed

Some content is hidden

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

60 files changed

+263
-274
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ Let's see what's so special about JavaScript, what we can achieve with it, and w
66

77
*JavaScript* was initially created to *"make web pages alive"*.
88

9-
The programs in this language are called *scripts*. They can be written right in a web page's HTML and executed automatically as the page loads.
9+
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

1111
Scripts are provided and executed as plain text. They don't need special preparation or compilation to run.
1212

@@ -70,7 +70,7 @@ Examples of such restrictions include:
7070
There are ways to interact with camera/microphone and other devices, but they require a user's explicit permission. So a JavaScript-enabled page may not sneakily enable a web-camera, observe the surroundings and send the information to the [NSA](https://en.wikipedia.org/wiki/National_Security_Agency).
7171
- Different tabs/windows generally do not know about each other. Sometimes they do, for example when one window uses JavaScript to open the other one. But even in this case, JavaScript from one page may not access the other if they come from different sites (from a different domain, protocol or port).
7272

73-
This is called the "Same Origin Policy". To work around that, *both pages* must contain a special JavaScript code that handles data exchange.
73+
This is called the "Same Origin Policy". To work around that, *both pages* must agree for data exchange and contain a special JavaScript code that handles it. We'll cover that in the tutorial.
7474

7575
This limitation is, again, for the user's safety. A page from `http://anysite.com` which a user has opened must not be able to access another browser tab with the URL `http://gmail.com` and steal information from there.
7676
- JavaScript can easily communicate over the net to the server where the current page came from. But its ability to receive data from other sites/domains is crippled. Though possible, it requires explicit agreement (expressed in HTTP headers) from the remote side. Once again, that's a safety limitation.

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ alert(message); // Hello world!
136136
```
137137
138138
```smart header="Functional languages"
139-
It's interesting to note that [functional](https://en.wikipedia.org/wiki/Functional_programming) programming languages, like [Scala](http://www.scala-lang.org/) or [Erlang](http://www.erlang.org/), forbid changing variable values.
139+
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.
140140
141141
In such languages, once the value is stored "in the box", it's there forever. If we need to store something else, the language forces us to create a new box (declare a new variable). We can't reuse the old one.
142142
@@ -182,7 +182,7 @@ let my-name; // hyphens '-' aren't allowed in the name
182182
Variables named `apple` and `AppLE` are two different variables.
183183
```
184184
185-
````smart header="Non-English letters are allowed, but not recommended"
185+
````smart header="Non-Latin letters are allowed, but not recommended"
186186
It is possible to use any language, including cyrillic letters or even hieroglyphs, like this:
187187
188188
```js
@@ -254,7 +254,7 @@ There is a widespread practice to use constants as aliases for difficult-to-reme
254254
255255
Such constants are named using capital letters and underscores.
256256
257-
Like this:
257+
For instance, let's make constants for colors in so-called "web" (hexadecimal) format:
258258
259259
```js run
260260
const COLOR_RED = "#F00";
@@ -290,7 +290,7 @@ In other words, capital-named constants are only used as aliases for "hard-coded
290290
291291
Talking about variables, there's one more extremely important thing.
292292
293-
Please name your variables sensibly. Take time to think about this.
293+
A variable name should have a clean, obvious meaning, describe the data that it stores.
294294
295295
Variable naming is one of the most important and complex skills in programming. A quick glance at variable names can reveal which code was written by a beginner versus an experienced developer.
296296

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ The last three lines may need additional explanation:
226226

227227
## Summary
228228

229-
There are 7 basic types in JavaScript.
229+
There are 7 basic data types in JavaScript.
230230

231231
- `number` for numbers of any kind: integer or floating-point.
232232
- `string` for strings. A string may have one or more characters, there's no separate single-character type.

1-js/02-first-steps/09-alert-prompt-confirm/article.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Interaction: alert, prompt, confirm
22

3-
This part of the tutorial aims to cover JavaScript "as is", without environment-specific tweaks.
3+
In this part of the tutorial we cover JavaScript language "as is", without environment-specific tweaks.
44

55
But we'll still be using the browser as our demo environment, so we should know at least a few of its user-interface functions. In this chapter, we'll get familiar with the browser functions `alert`, `prompt` and `confirm`.
66

1-js/02-first-steps/10-ifelse/article.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
Sometimes, we need to perform different actions based on different conditions.
44

5-
To do that, we use the `if` statement and the conditional (ternary) operator which we will be referring to as the “question mark operator `?` for simplicity.
5+
To do that, we can use the `if` statement and the conditional operator `?`, that's also called a "question mark" operator.
66

77
## The "if" statement
88

@@ -103,7 +103,7 @@ In the code above, JavaScript first checks `year < 2015`. If that is falsy, it g
103103

104104
There can be more `else if` blocks. The final `else` is optional.
105105

106-
## Ternary operator '?'
106+
## Conditional operator '?'
107107

108108
Sometimes, we need to assign a variable depending on a condition.
109109

@@ -124,9 +124,9 @@ if (age > 18) {
124124
alert(accessAllowed);
125125
```
126126

127-
The so-called "ternary" or "question mark" operator lets us do that in a shorter and simpler way.
127+
The so-called "conditional" or "question mark" operator lets us do that in a shorter and simpler way.
128128

129-
The operator is represented by a question mark `?`. The formal term "ternary" means that the operator has three operands. It is actually the one and only operator in JavaScript which has that many.
129+
The operator is represented by a question mark `?`. Sometimes it's called "ternary", because the operator has three operands. It is actually the one and only operator in JavaScript which has that many.
130130

131131
The syntax is:
132132
```js
@@ -141,7 +141,7 @@ For example:
141141
let accessAllowed = (age > 18) ? true : false;
142142
```
143143

144-
Technically, we can omit the parentheses around `age > 18`. The question mark operator has a low precedence, so it executes after the comparison `>`.
144+
Technically, we can omit the parentheses around `age > 18`. The question mark operator has a low precedence, so it executes after the comparison `>`.
145145

146146
This example will do the same thing as the previous one:
147147

1-js/02-first-steps/11-logical-operators/article.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ The OR `||` operator does the following:
8484

8585
A value is returned in its original form, without the conversion.
8686

87-
In other words, a chain of OR `"||"` returns the first truthy value or the last one if no such value is found.
87+
In other words, a chain of OR `"||"` returns the first truthy value or the last one if no truthy value is found.
8888

8989
For instance:
9090

@@ -101,7 +101,7 @@ This leads to some interesting usage compared to a "pure, classical, boolean-onl
101101

102102
1. **Getting the first truthy value from a list of variables or expressions.**
103103

104-
Imagine we have several variables which can either contain data or be `null/undefined`. How can we find the first one with data?
104+
Imagine we have a list of variables which can either contain data or be `null/undefined`. How can we find the first one with data?
105105

106106
We can use OR `||`:
107107

@@ -143,7 +143,7 @@ This leads to some interesting usage compared to a "pure, classical, boolean-onl
143143
alert(x); // 1
144144
```
145145

146-
An assignment is a simple case. Other side effects can also be involved.
146+
An assignment is a simple case. There may be side effects, that won't show up if the evaluation doesn't reach them.
147147

148148
As we can see, such a use case is a "shorter way of doing `if`". The first operand is converted to boolean. If it's false, the second one is evaluated.
149149

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ showMessage();
101101
alert( userName ); // *!*Bob*/!*, the value was modified by the function
102102
```
103103

104-
The outer variable is only used if there's no local one. So an occasional modification may happen if we forget `let`.
104+
The outer variable is only used if there's no local one.
105105

106106
If a same-named variable is declared inside the function then it *shadows* the outer one. For instance, in the code below the function uses the local `userName`. The outer one is ignored:
107107

@@ -128,7 +128,7 @@ Variables declared outside of any function, such as the outer `userName` in the
128128
129129
Global variables are visible from any function (unless shadowed by locals).
130130
131-
Usually, a function declares all variables specific to its task. Global variables only store project-level data, and it's important that these variables are accessible from anywhere. Modern code has few or no globals. Most variables reside in their functions.
131+
It's a good practice to minimize the use of global variables. Modern code has few or no globals. Most variables reside in their functions. Sometimes though, they can be useful to store project-level data.
132132
```
133133

134134
## Parameters
@@ -376,7 +376,7 @@ A few examples of breaking this rule:
376376
- `createForm` -- would be bad if it modifies the document, adding a form to it (should only create it and return).
377377
- `checkPermission` -- would be bad if it displays the `access granted/denied` message (should only perform the check and return the result).
378378

379-
These examples assume common meanings of prefixes. What they mean for you is determined by you and your team. Maybe it's pretty normal for your code to behave differently. But you should have a firm understanding of what a prefix means, what a prefixed function can and cannot do. All same-prefixed functions should obey the rules. And the team should share the knowledge.
379+
These examples assume common meanings of prefixes. You and your team are free to agree on other meanings, but usually they're not much different. In any case, you should have a firm understanding of what a prefix means, what a prefixed function can and cannot do. All same-prefixed functions should obey the rules. And the team should share the knowledge.
380380
```
381381

382382
```smart header="Ultrashort function names"

1-js/02-first-steps/16-javascript-specials/article.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ More in: <info:variables> and <info:types>.
103103
We're using a browser as a working environment, so basic UI functions will be:
104104

105105
[`prompt(question, [default])`](mdn:api/Window/prompt)
106-
: Ask a `question`, and return either what the visitor entered or `null` if they pressed "cancel".
106+
: Ask a `question`, and return either what the visitor entered or `null` if they clicked "cancel".
107107

108108
[`confirm(question)`](mdn:api/Window/confirm)
109109
: Ask a `question` and suggest to choose between Ok and Cancel. The choice is returned as `true/false`.
@@ -161,7 +161,7 @@ Comparisons
161161

162162
Other comparisons convert to a number as well.
163163

164-
The strict equality operator `===` doesn't do the conversion: different types always mean different values for it, so:
164+
The strict equality operator `===` doesn't do the conversion: different types always mean different values for it.
165165

166166
Values `null` and `undefined` are special: they equal `==` each other and don't equal anything else.
167167

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ Here's what you should see if you are doing it for the first time:
2020

2121
The toggler button <span class="devtools" style="background-position:-168px -76px"></span> opens the tab with files.
2222

23-
Let's click it and select `index.html` and then `hello.js` in the tree view. Here's what should show up:
23+
Let's click it and select `hello.js` in the tree view. Here's what should show up:
2424

2525
![](chrome-tabs.png)
2626

@@ -34,7 +34,7 @@ Now you could click the same toggler <span class="devtools" style="background-po
3434

3535
## Console
3636

37-
If we press `Esc`, then a console opens below. We can type commands there and press `key:Enter` to execute.
37+
If we press `key:Esc`, then a console opens below. We can type commands there and press `key:Enter` to execute.
3838

3939
After a statement is executed, its result is shown below.
4040

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

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,21 +19,20 @@ Here Babel comes to the rescue.
1919

2020
Actually, there are two parts in Babel:
2121

22-
1. First, the transpiler program, which rewrites the code. The developer runs it on their own computer. It rewrites the code into the older standard. And then the code is delivered to the website for users. Modern project build system like [webpack](http://webpack.github.io/) or [brunch](http://brunch.io/) provide means to run transpiler automatically on every code change, so that doesn't involve any time loss from our side.
22+
1. First, the transpiler program, which rewrites the code. The developer runs it on their own computer. It rewrites the code into the older standard. And then the code is delivered to the website for users. Modern project build system like [webpack](http://webpack.github.io/) provide means to run transpiler automatically on every code change, so that very easy to integrate into development process.
2323

2424
2. Second, the polyfill.
2525

26-
The transpiler rewrites the code, so syntax features are covered. But for new functions we need to write a special script that implements them. JavaScript is a highly dynamic language, scripts may not just add new functions, but also modify built-in ones, so that they behave according to the modern standard.
26+
New language features may include new built-in functions and syntax constructs.
27+
The transpiler rewrites the code, transforming syntax constructs into older ones. But as for new built-in functions, we need to implement them. JavaScript is a highly dynamic language, scripts may add/modify any functions, so that they behave according to the modern standard.
2728

28-
There's a term "polyfill" for scripts that "fill in" the gap and add missing implementations.
29+
A script that updates/adds new functions is called "polyfill". It "fills in" the gap and adds missing implementations.
2930

3031
Two interesting polyfills are:
3132
- [babel polyfill](https://babeljs.io/docs/usage/polyfill/) that supports a lot, but is big.
3233
- [polyfill.io](http://polyfill.io) service that allows to load/construct polyfills on-demand, depending on the features we need.
3334

34-
So, we need to setup the transpiler and add the polyfill for old engines to support modern features.
35-
36-
If we orient towards modern engines and do not use features except those supported everywhere, then we don't need to use Babel.
35+
So, if we're going to use modern language features, a transpiler and a polyfill are necessary.
3736

3837
## Examples in the tutorial
3938

@@ -49,9 +48,7 @@ Examples that use modern JS will work only if your browser supports it.
4948
````
5049

5150
```offline
52-
As you're reading the offline version, examples are not runnable. But they usually work :)
51+
As you're reading the offline version, in PDF examples are not runnable. In EPUB some of them can run.
5352
```
5453

55-
[Chrome Canary](https://www.google.com/chrome/browser/canary.html) is good for all examples, but other modern browsers are mostly fine too.
56-
57-
Note that on production we can use Babel to translate the code into suitable for less recent browsers, so there will be no such limitation, the code will run everywhere.
54+
Google Chrome is usually the most up-to-date with language features, good to run bleeding-edge demos without any transpilers, but other modern browsers also work fine.

1-js/04-object-basics/06-constructor-new/article.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ let user = new function() {
8383
The constructor can't be called again, because it is not saved anywhere, just created and called. So this trick aims to encapsulate the code that constructs the single object, without future reuse.
8484
````
8585

86-
## Dual-syntax constructors: new.target
86+
## Constructor mode test: new.target
8787

8888
```smart header="Advanced stuff"
8989
The syntax from this section is rarely used, skip it unless you want to know everything.
@@ -109,7 +109,9 @@ new User(); // function User { ... }
109109
*/!*
110110
```
111111

112-
That can be used to allow both `new` and regular calls to work the same. That is, create the same object:
112+
That can be used inside the function to know whether it was called with `new`, "in constructor mode", or without it, "in regular mode".
113+
114+
We can also make both `new` and regular calls to do the same, like this:
113115

114116
```js run
115117
function User(name) {

1-js/05-data-types/01-primitives-methods/1-string-new-property/solution.md

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,26 +6,21 @@ let str = "Hello";
66

77
str.test = 5; // (*)
88

9-
alert(str.test);
9+
alert(str.test);
1010
```
1111

12-
There may be two kinds of result:
13-
1. `undefined`
14-
2. An error.
12+
Depending on whether you have `use strict` or not, the result may be:
13+
1. `undefined` (no strict mode)
14+
2. An error (strict mode).
1515

1616
Why? Let's replay what's happening at line `(*)`:
1717

1818
1. When a property of `str` is accessed, a "wrapper object" is created.
19-
2. The operation with the property is carried out on it. So, the object gets the `test` property.
20-
3. The operation finishes and the "wrapper object" disappears.
19+
2. In strict mode, writing into it is an error.
20+
3. Otherwise, the operation with the property is carried on, the object gets the `test` property, but after that the "wrapper object" disappears.
2121

22-
So, on the last line, `str` has no trace of the property. A new wrapper object for every object operation on a string.
23-
24-
Some browsers though may decide to further limit the programmer and disallow to assign properties to primitives at all. That's why in practice we can also see errors at line `(*)`. It's a little bit farther from the specification though.
22+
So, without strict mode, in the last line `str` has no trace of the property.
2523

2624
**This example clearly shows that primitives are not objects.**
2725

28-
They just can not store data.
29-
30-
All property/method operations are performed with the help of temporary objects.
31-
26+
They can't store additional data.

1-js/05-data-types/01-primitives-methods/article.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ A primitive
1414
An object
1515

1616
- Is capable of storing multiple values as properties.
17-
- Can be created with `{}`, for instance: `{name: "John", age: 30}`. There are other kinds of objects in JavaScript; functions, for example, are objects.
17+
- Can be created with `{}`, for instance: `{name: "John", age: 30}`. There are other kinds of objects in JavaScript: functions, for example, are objects.
1818

1919
One of the best things about objects is that we can store a function as one of its properties.
2020

@@ -48,7 +48,7 @@ The solution looks a little bit awkward, but here it is:
4848

4949
1. Primitives are still primitive. A single value, as desired.
5050
2. The language allows access to methods and properties of strings, numbers, booleans and symbols.
51-
3. When this happens, a special "object wrapper" that provides the extra functionality is created, and then is destroyed.
51+
3. In order for that to work, a special "object wrapper" that provides the extra functionality is created, and then is destroyed.
5252

5353
The "object wrappers" are different for each primitive type and are called: `String`, `Number`, `Boolean` and `Symbol`. Thus, they provide different sets of methods.
5454

@@ -91,18 +91,18 @@ In JavaScript, that's also possible for historical reasons, but highly **unrecom
9191
For instance:
9292

9393
```js run
94-
alert( typeof 1 ); // "number"
94+
alert( typeof 0 ); // "number"
9595

96-
alert( typeof new Number(1) ); // "object"!
96+
alert( typeof new Number(0) ); // "object"!
9797
```
9898

99-
And because what follows, `zero`, is an object, the alert will show up:
99+
Objects are always truthy in `if`, so here the alert will show up:
100100

101101
```js run
102102
let zero = new Number(0);
103103

104104
if (zero) { // zero is true, because it's an object
105-
alert( "zero is truthy?!?" );
105+
alert( "zero is truthy!?!" );
106106
}
107107
```
108108

0 commit comments

Comments
 (0)