Skip to content

Commit a16294f

Browse files
Merge pull request #2 from javascript-tutorial/master
Minor typos
2 parents f62b296 + e101ed7 commit a16294f

File tree

93 files changed

+576
-410
lines changed

Some content is hidden

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

93 files changed

+576
-410
lines changed

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

-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88

99
മിക്ക ഡവലപ്പർമാരും ഡെവലപ്‌മെന്റിനായി Chrome അല്ലെങ്കിൽ Firefox ആയിരിക്കുo ആശ്രയിക്കുന്നത്, കാരണം ആ ബ്രൗസറുകളിൽ മികച്ച ഡവലപ്പർ ടൂൾസ് ഉൾപ്പെടുത്തിയിട്ടുണ്ട്. മറ്റ് ബ്രവ്സറുകളും ഡെവലപ്പർ ടൂൾസ് നൽകുന്നു, ചിലപ്പോൾ ചില പ്രത്യേകതകളും അവ നൽകും, പക്ഷേ സാധാരണയായി Chrome അല്ലെങ്കിൽ Firefox ലോട്ടു അനുകരിക്കുകയാണ് അവ ചെയ്യുന്നത്. അതിനാൽ മിക്ക ഡവലപ്പർമാർക്കും അവർക്ക് "ഇഷ്ടപ്പെട്ട" ഒരു ബ്രൗസർ കാണും കൂടാതെ ഒരു ബ്രൗസറിൽ എന്തെങ്കിലും പ്രശ്നമുള്ളതായി തോന്നുകയാണെങ്കിൽ മറ്റുള്ളവയിലേക്കു മാറുകയോ ചെയ്യും.
1010

11-
1211
ഡവലപ്പർ ടൂൾസ് വളരെ ശക്തമാണ്; അവയ്‌ക്ക് നിരവധി സവിശേഷതകളുണ്ട്. നമുക്കിപ്പോൾ, അവ എങ്ങനെ ഓപ്പൺ ചെയ്യാമെന്നും, തെറ്റുകൾ എങ്ങനെ നോക്കാമെന്നും, javascript command കൾ എങ്ങനെ ടെസ്റ്റ് ചെയ്തു നോക്കാമെന്നും പഠിക്കാം.
1312

1413

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

+16-20
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

+1-1
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/3-primitive-conversions-questions/solution.md

-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ true + false = 1
99
"$" + 4 + 5 = "$45"
1010
"4" - 2 = 2
1111
"4px" - 2 = NaN
12-
7 / 0 = Infinity
1312
" -9 " + 5 = " -9 5" // (3)
1413
" -9 " - 5 = -14 // (4)
1514
null + 1 = 1 // (5)

1-js/02-first-steps/08-operators/3-primitive-conversions-questions/task.md

-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ true + false
1616
"$" + 4 + 5
1717
"4" - 2
1818
"4px" - 2
19-
7 / 0
2019
" -9 " + 5
2120
" -9 " - 5
2221
null + 1

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

+9-5
Original file line numberDiff line numberDiff line change
@@ -56,17 +56,21 @@ alert( 8 % 3 ); // 2, a remainder of 8 divided by 3
5656

5757
### Exponentiation **
5858

59-
The exponentiation operator `a ** b` multiplies `a` by itself `b` times.
59+
The exponentiation operator `a ** b` raises `a` to the power of `b`.
60+
61+
In school maths, we write that as a<sup>b</sup>.
6062

6163
For instance:
6264

6365
```js run
64-
alert( 2 ** 2 ); // 4 (2 multiplied by itself 2 times)
65-
alert( 2 ** 3 ); // 8 (2 * 2 * 2, 3 times)
66-
alert( 2 ** 4 ); // 16 (2 * 2 * 2 * 2, 4 times)
66+
alert( 2 ** 2 ); // 2² = 4
67+
alert( 2 ** 3 ); // 2³ = 8
68+
alert( 2 ** 4 ); // 2⁴ = 16
6769
```
6870

69-
Mathematically, the exponentiation is defined for non-integer numbers as well. For example, a square root is an exponentiation by `1/2`:
71+
Just like in maths, the exponentiation operator is defined for non-integer numbers as well.
72+
73+
For example, a square root is an exponentiation by ½:
7074

7175
```js run
7276
alert( 4 ** (1/2) ); // 2 (power of 1/2 is the same as a square root)

1-js/02-first-steps/10-ifelse/2-check-standard/task.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ importance: 2
66

77
Using the `if..else` construct, write the code which asks: 'What is the "official" name of JavaScript?'
88

9-
If the visitor enters "ECMAScript", then output "Right!", otherwise -- output: "Didn't know? ECMAScript!"
9+
If the visitor enters "ECMAScript", then output "Right!", otherwise -- output: "You don't know? ECMAScript!"
1010

1111
![](ifelse_task2.svg)
1212

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Logical operators
22

3-
There are three logical operators in JavaScript: `||` (OR), `&&` (AND), `!` (NOT).
3+
There are four logical operators in JavaScript: `||` (OR), `&&` (AND), `!` (NOT), `??` (Nullish Coalescing). Here we cover the first three, the `??` operator is in the next article.
44

55
Although they are called "logical", they can be applied to values of any type, not only boolean. Their result can also be of any type.
66

@@ -64,7 +64,7 @@ if (hour < 10 || hour > 18 || isWeekend) {
6464
}
6565
```
6666

67-
## OR "||" finds the first truthy value
67+
## OR "||" finds the first truthy value [#or-finds-the-first-truthy-value]
6868

6969
The logic described above is somewhat classical. Now, let's bring in the "extra" features of JavaScript.
7070

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

+18-15
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,14 @@
22

33
[recent browser="new"]
44

5-
Here, in this article, we'll say that an expression is "defined" when it's neither `null` nor `undefined`.
6-
75
The nullish coalescing operator is written as two question marks `??`.
86

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`.
8+
99
The result of `a ?? b` is:
1010
- if `a` is defined, then `a`,
1111
- if `a` isn't defined, then `b`.
1212

13-
1413
In other words, `??` returns the first argument if it's not `null/undefined`. Otherwise, the second one.
1514

1615
The nullish coalescing operator isn't anything completely new. It's just a nice syntax to get the first "defined" value of the two.
@@ -21,29 +20,31 @@ We can rewrite `result = a ?? b` using the operators that we already know, like
2120
result = (a !== null && a !== undefined) ? a : b;
2221
```
2322

23+
Now it should be absolutely clear what `??` does. Let's see where it helps.
24+
2425
The common use case for `??` is to provide a default value for a potentially undefined variable.
2526

26-
For example, here we show `Anonymous` if `user` isn't defined:
27+
For example, here we show `user` if defined, otherwise `Anonymous`:
2728

2829
```js run
2930
let user;
3031

31-
alert(user ?? "Anonymous"); // Anonymous
32+
alert(user ?? "Anonymous"); // Anonymous (user not defined)
3233
```
3334
34-
Of course, if `user` had any value except `null/undefined`, then we would see it instead:
35+
Here's the example with `user` assigned to a name:
3536
3637
```js run
3738
let user = "John";
3839

39-
alert(user ?? "Anonymous"); // John
40+
alert(user ?? "Anonymous"); // John (user defined)
4041
```
4142
4243
We can also use a sequence of `??` to select the first value from a list that isn't `null/undefined`.
4344
44-
Let's say we have a user's data in variables `firstName`, `lastName` or `nickName`. All of them may be undefined, 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 enter a value.
4546
46-
We'd like to display the user name using one of these variables, or show "Anonymous" if all of them are undefined.
47+
We'd like to display the user name using one of these variables, or show "Anonymous" if all of them aren't defined.
4748
4849
Let's use the `??` operator for that:
4950
@@ -75,7 +76,7 @@ alert(firstName || lastName || nickName || "Anonymous"); // Supercoder
7576
*/!*
7677
```
7778
78-
The OR `||` operator exists since the beginning of JavaScript, so developers were using it for such purposes for a long time.
79+
Historically, the OR `||` operator was there first. It exists since the beginning of JavaScript, so developers were using it for such purposes for a long time.
7980
8081
On the other hand, the nullish coalescing operator `??` was added to JavaScript only recently, and the reason for that was that people weren't quite happy with `||`.
8182
@@ -96,16 +97,18 @@ alert(height || 100); // 100
9697
alert(height ?? 100); // 0
9798
```
9899
99-
- The `height || 100` checks `height` for being a falsy value, and it really is.
100-
- so the result is the second argument, `100`.
100+
- The `height || 100` checks `height` for being a falsy value, and it's `0`, falsy indeed.
101+
- so the result of `||` is the second argument, `100`.
101102
- The `height ?? 100` checks `height` for being `null/undefined`, and it's not,
102103
- so the result is `height` "as is", that is `0`.
103104
104-
If the zero height is a valid value, that shouldn't be replaced with the default, then `??` does just the right thing.
105+
In practice, the zero height is often a valid value, that shouldn't be replaced with the default. So `??` does just the right thing.
105106
106107
## Precedence
107108
108-
The precedence of the `??` operator is rather low: `5` in the [MDN table](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Operator_Precedence#Table). So `??` is evaluated before `=` and `?`, but after most other operations, such as `+`, `*`.
109+
The precedence of the `??` operator is about the same as `||`, just a bit lower. It equals `5` in the [MDN table](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Operator_Precedence#Table), while `||` is `6`.
110+
111+
That means that, just like `||`, the nullish coalescing operator `??` is evaluated before `=` and `?`, but after most other operations, such as `+`, `*`.
109112
110113
So if we'd like to choose a value with `??` in an expression with other operators, consider adding parentheses:
111114
@@ -139,7 +142,7 @@ The code below triggers a syntax error:
139142
let x = 1 && 2 ?? 3; // Syntax error
140143
```
141144
142-
The limitation is surely debatable, but it was added to the language specification with the purpose to avoid programming mistakes, when people start to switch to `??` from `||`.
145+
The limitation is surely debatable, it was added to the language specification with the purpose to avoid programming mistakes, when people start to switch from `||` to `??`.
143146
144147
Use explicit parentheses to work around it:
145148

1-js/02-first-steps/13-while-for/article.md

+14-3
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,7 @@ alert('Done!');
318318

319319
We need a way to stop the process if the user cancels the input.
320320

321-
The ordinary `break` after `input` would only break the inner loop. That's not sufficient--labels, come to the rescue!
321+
The ordinary `break` after `input` would only break the inner loop. That's not sufficient -- labels, come to the rescue!
322322

323323
A *label* is an identifier with a colon before a loop:
324324
```js
@@ -363,12 +363,23 @@ Labels do not allow us to jump into an arbitrary place in the code.
363363
364364
For example, it is impossible to do this:
365365
```js
366-
break label; // doesn't jumps to the label below
366+
break label; // jump to the label below (doesn't work)
367367
368368
label: for (...)
369369
```
370370
371-
A call to `break/continue` is only possible from inside a loop and the label must be somewhere above the directive.
371+
A `break` directive must be inside a code block. Technically, any labelled code block will do, e.g.:
372+
```js
373+
label: {
374+
// ...
375+
break label; // works
376+
// ...
377+
}
378+
```
379+
380+
...Although, 99.9% of the time `break` used is inside loops, as we've seen in the examples above.
381+
382+
A `continue` is only possible from inside a loop.
372383
````
373384

374385
## Summary

1-js/02-first-steps/15-function-basics/2-rewrite-function-question-or/solution.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,4 @@ function checkAge(age) {
1414
}
1515
```
1616

17-
Note that the parentheses around `age > 18` are not required here. They exist for better readabilty.
17+
Note that the parentheses around `age > 18` are not required here. They exist for better readability.

0 commit comments

Comments
 (0)