Skip to content

Commit c0de135

Browse files
committed
Merge branch 'master' of github.com:javascript-tutorial/en.javascript.info into sync-291b5c05
2 parents 3622889 + 291b5c0 commit c0de135

File tree

38 files changed

+194
-112
lines changed

38 files changed

+194
-112
lines changed

1-js/02-first-steps/07-type-conversions/article.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ For example, `alert` automatically converts any value to a string to show it. Ma
77
There are also cases when we need to explicitly convert a value to the expected type.
88

99
```smart header="Not talking about objects yet"
10-
In this chapter, we won't cover objects. For now we'll just be talking about primitives.
10+
In this chapter, we won't cover objects. For now, we'll just be talking about primitives.
1111
1212
Later, after we learn about objects, in the chapter <info:object-toprimitive> we'll see how objects fit in.
1313
```

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -437,7 +437,7 @@ The list of operators:
437437
- RIGHT SHIFT ( `>>` )
438438
- ZERO-FILL RIGHT SHIFT ( `>>>` )
439439
440-
These operators are used very rarely, when we need to fiddle with numbers on the very lowest (bitwise) level. We won't need these operators any time soon, as web development has little use of them, but in some special areas, such as cryptography, they are useful. You can read the [Bitwise Operators](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Expressions_and_Operators#Bitwise) chapter on MDN when a need arises.
440+
These operators are used very rarely, when we need to fiddle with numbers on the very lowest (bitwise) level. We won't need these operators any time soon, as web development has little use of them, but in some special areas, such as cryptography, they are useful. You can read the [Bitwise Operators](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Expressions_and_Operators#bitwise_operators) chapter on MDN when a need arises.
441441

442442
## Comma
443443

1-js/02-first-steps/12-nullish-coalescing-operator/article.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
The nullish coalescing operator is written as two question marks `??`.
66

7-
As it treats `null` and `undefined` similarly, we'll use a special term here, in this article. We'll say that an expression is "defined" when it's neither `null` nor `undefined`.
7+
As it treats `null` and `undefined` similarly, we'll use a special term here, in this article. For brevity, we'll say that a value is "defined" when it's neither `null` nor `undefined`.
88

99
The result of `a ?? b` is:
1010
- if `a` is defined, then `a`,
@@ -22,9 +22,9 @@ result = (a !== null && a !== undefined) ? a : b;
2222

2323
Now it should be absolutely clear what `??` does. Let's see where it helps.
2424

25-
The common use case for `??` is to provide a default value for a potentially undefined variable.
25+
The common use case for `??` is to provide a default value.
2626

27-
For example, here we show `user` if defined, otherwise `Anonymous`:
27+
For example, here we show `user` if its value isn't `null/undefined`, otherwise `Anonymous`:
2828

2929
```js run
3030
let user;
@@ -42,9 +42,9 @@ alert(user ?? "Anonymous"); // John (user defined)
4242
4343
We can also use a sequence of `??` to select the first value from a list that isn't `null/undefined`.
4444
45-
Let's say we have a user's data in variables `firstName`, `lastName` or `nickName`. All of them may be not defined, if the user decided not to enter a value.
45+
Let's say we have a user's data in variables `firstName`, `lastName` or `nickName`. All of them may be not defined, if the user decided not to fill in the corresponding values.
4646
47-
We'd like to display the user name using one of these variables, or show "Anonymous" if all of them aren't defined.
47+
We'd like to display the user name using one of these variables, or show "Anonymous" if all of them are `null/undefined`.
4848
4949
Let's use the `??` operator for that:
5050
@@ -110,7 +110,7 @@ The precedence of the `??` operator is the same as `||`. They both equal `4` in
110110
111111
That means that, just like `||`, the nullish coalescing operator `??` is evaluated before `=` and `?`, but after most other operations, such as `+`, `*`.
112112
113-
So if we'd like to choose a value with `??` in an expression with other operators, consider adding parentheses:
113+
So we may need to add parentheses in expressions like this:
114114
115115
```js run
116116
let height = null;
@@ -128,7 +128,7 @@ Otherwise, if we omit parentheses, then as `*` has the higher precedence than `?
128128
// without parentheses
129129
let area = height ?? 100 * width ?? 50;
130130

131-
// ...works the same as this (probably not what we want):
131+
// ...works this way (not what we want):
132132
let area = height ?? (100 * width) ?? 50;
133133
```
134134
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1-
No difference.
1+
No difference!
2+
3+
In both cases, `return confirm('Did parents allow you?')` executes exactly when the `if` condition is falsy.

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

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ When a value is passed as a function parameter, it's also called an *argument*.
176176
177177
In other words, to put these terms straight:
178178
179-
- A parameter is the variable listed inside the parentheses in the function declaration (it's a declaration time term)
179+
- A parameter is the variable listed inside the parentheses in the function declaration (it's a declaration time term).
180180
- An argument is the value that is passed to the function when it is called (it's a call time term).
181181
182182
We declare functions listing their parameters, then call them passing arguments.
@@ -225,6 +225,38 @@ In the example above, `anotherFunction()` isn't called at all, if the `text` par
225225
On the other hand, it's independently called every time when `text` is missing.
226226
```
227227

228+
````smart header="Default parameters in old JavaScript code"
229+
Several years ago, JavaScript didn't support the syntax for default parameters. So people used other ways to specify them.
230+
231+
Nowadays, we can come across them in old scripts.
232+
233+
For example, an explicit check for `undefined`:
234+
235+
```js
236+
function showMessage(from, text) {
237+
*!*
238+
if (text === undefined) {
239+
text = 'no text given';
240+
}
241+
*/!*
242+
243+
alert( from + ": " + text );
244+
}
245+
```
246+
247+
...Or using the `||` operator:
248+
249+
```js
250+
function showMessage(from, text) {
251+
// If the value of text is falsy, assign the default value
252+
// this assumes that text == "" is the same as no text at all
253+
text = text || 'no text given';
254+
...
255+
}
256+
```
257+
````
258+
259+
228260
### Alternative default parameters
229261
230262
Sometimes it makes sense to assign default values for parameters not in the function declaration, but at a later stage.

1-js/02-first-steps/16-function-expressions/article.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ First, the syntax: how to differentiate between them in the code.
194194
return a + b;
195195
}
196196
```
197-
- *Function Expression:* a function, created inside an expression or inside another syntax construct. Here, the function is created at the right side of the "assignment expression" `=`:
197+
- *Function Expression:* a function, created inside an expression or inside another syntax construct. Here, the function is created on the right side of the "assignment expression" `=`:
198198

199199
```js
200200
// Function Expression
@@ -291,7 +291,7 @@ if (age < 18) {
291291
welcome(); // \ (runs)
292292
*/!*
293293
// |
294-
function welcome() { // |
294+
function welcome() { // |
295295
alert("Hello!"); // | Function Declaration is available
296296
} // | everywhere in the block where it's declared
297297
// |
@@ -301,7 +301,7 @@ if (age < 18) {
301301

302302
} else {
303303

304-
function welcome() {
304+
function welcome() {
305305
alert("Greetings!");
306306
}
307307
}

1-js/02-first-steps/17-arrow-functions-basics/article.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ For instance, to dynamically create a function:
6464
let age = prompt("What is your age?", 18);
6565
6666
let welcome = (age < 18) ?
67-
() => alert('Hello') :
67+
() => alert('Hello!') :
6868
() => alert("Greetings!");
6969
7070
welcome();

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@ We covered three ways to create a function in JavaScript:
256256
3. Arrow functions:
257257
258258
```js
259-
// expression at the right side
259+
// expression on the right side
260260
let sum = (a, b) => a + b;
261261
262262
// or multi-line syntax with { ... }, need return here:

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

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ The resulting `user` object can be imagined as a cabinet with two signed files l
4444

4545
![user object](object-user.svg)
4646

47-
We can add, remove and read files from it any time.
47+
We can add, remove and read files from it at any time.
4848

4949
Property values are accessible using the dot notation:
5050

@@ -62,7 +62,7 @@ user.isAdmin = true;
6262

6363
![user object 2](object-user-isadmin.svg)
6464

65-
To remove a property, we can use `delete` operator:
65+
To remove a property, we can use the `delete` operator:
6666

6767
```js
6868
delete user.age;
@@ -201,13 +201,13 @@ let bag = {
201201
};
202202
```
203203

204-
Square brackets are much more powerful than the dot notation. They allow any property names and variables. But they are also more cumbersome to write.
204+
Square brackets are much more powerful than dot notation. They allow any property names and variables. But they are also more cumbersome to write.
205205

206206
So most of the time, when property names are known and simple, the dot is used. And if we need something more complex, then we switch to square brackets.
207207

208208
## Property value shorthand
209209

210-
In real code we often use existing variables as values for property names.
210+
In real code, we often use existing variables as values for property names.
211211

212212
For instance:
213213

@@ -252,7 +252,7 @@ let user = {
252252

253253
## Property names limitations
254254

255-
As we already know, a variable cannot have a name equal to one of language-reserved words like "for", "let", "return" etc.
255+
As we already know, a variable cannot have a name equal to one of the language-reserved words like "for", "let", "return" etc.
256256

257257
But for an object property, there's no such restriction:
258258

@@ -325,7 +325,7 @@ alert( "blabla" in user ); // false, user.blabla doesn't exist
325325

326326
Please note that on the left side of `in` there must be a *property name*. That's usually a quoted string.
327327

328-
If we omit quotes, that means a variable, it should contain the actual name to be tested. For instance:
328+
If we omit quotes, that means a variable should contain the actual name to be tested. For instance:
329329

330330
```js run
331331
let user = { age: 30 };
@@ -412,7 +412,7 @@ for (let code in codes) {
412412
*/!*
413413
```
414414
415-
The object may be used to suggest a list of options to the user. If we're making a site mainly for German audience then we probably want `49` to be the first.
415+
The object may be used to suggest a list of options to the user. If we're making a site mainly for a German audience then we probably want `49` to be the first.
416416
417417
But if we run the code, we see a totally different picture:
418418
@@ -424,9 +424,10 @@ The phone codes go in the ascending sorted order, because they are integers. So
424424
````smart header="Integer properties? What's that?"
425425
The "integer property" term here means a string that can be converted to-and-from an integer without a change.
426426
427-
So, "49" is an integer property name, because when it's transformed to an integer number and back, it's still the same. But "+49" and "1.2" are not:
427+
So, `"49"` is an integer property name, because when it's transformed to an integer number and back, it's still the same. But `"+49"` and `"1.2"` are not:
428428
429429
```js run
430+
// Number(...) explicitly converts to a number
430431
// Math.trunc is a built-in function that removes the decimal part
431432
alert( String(Math.trunc(Number("49"))) ); // "49", same, integer property
432433
alert( String(Math.trunc(Number("+49"))) ); // "49", not same "+49" ⇒ not integer property
@@ -481,7 +482,7 @@ They store properties (key-value pairs), where:
481482
482483
To access a property, we can use:
483484
- The dot notation: `obj.property`.
484-
- Square brackets notation `obj["property"]`. Square brackets allow to take the key from a variable, like `obj[varWithKey]`.
485+
- Square brackets notation `obj["property"]`. Square brackets allow taking the key from a variable, like `obj[varWithKey]`.
485486
486487
Additional operators:
487488
- To delete a property: `delete obj.prop`.

1-js/04-object-basics/04-object-methods/7-calculator/task.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ importance: 5
66

77
Create an object `calculator` with three methods:
88

9-
- `read()` prompts for two values and saves them as object properties.
9+
- `read()` prompts for two values and saves them as object properties with names `a` and `b` respectively.
1010
- `sum()` returns the sum of saved values.
1111
- `mul()` multiplies saved values and returns the result.
1212

@@ -21,4 +21,3 @@ alert( calculator.mul() );
2121
```
2222

2323
[demo]
24-

0 commit comments

Comments
 (0)