You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: 1-js/01-getting-started/1-intro/article.md
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -26,7 +26,7 @@ Different engines have different "codenames". For example:
26
26
27
27
-[V8](https://en.wikipedia.org/wiki/V8_(JavaScript_engine)) -- in Chrome and Opera.
28
28
-[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.
30
30
31
31
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.
Copy file name to clipboardExpand all lines: 1-js/01-getting-started/4-devtools/article.md
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -8,7 +8,7 @@ To see errors and get a lot of other useful information about scripts, "develope
8
8
9
9
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.
10
10
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.
Copy file name to clipboardExpand all lines: 1-js/02-first-steps/02-structure/article.md
+16-20Lines changed: 16 additions & 20 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -46,7 +46,7 @@ alert(3 +
46
46
+2);
47
47
```
48
48
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.
50
50
51
51
**But there are situations where JavaScript "fails" to assume a semicolon where it is really needed.**
52
52
@@ -56,40 +56,36 @@ Errors which occur in such cases are quite hard to find and fix.
56
56
If you're curious to see a concrete example of such an error, check this code out:
57
57
58
58
```js run
59
-
[1, 2].forEach(alert)
59
+
alert("Hello");
60
+
61
+
[1, 2].forEach(alert);
60
62
```
61
63
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`.
63
65
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`:
65
67
66
68
```js run no-beautify
67
-
alert("There will be an error")
69
+
alert("Hello")
68
70
69
-
[1, 2].forEach(alert)
71
+
[1, 2].forEach(alert);
70
72
```
71
73
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.
77
75
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.
80
77
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.
82
79
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:
87
81
88
82
```js run no-beautify
89
-
alert("There will be an error")[1, 2].forEach(alert)
83
+
alert("Hello")[1, 2].forEach(alert);
90
84
```
91
85
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.
93
89
````
94
90
95
91
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.
Copy file name to clipboardExpand all lines: 1-js/02-first-steps/05-types/article.md
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -64,7 +64,7 @@ Special numeric values formally belong to the "number" type. Of course they are
64
64
65
65
We'll see more about working with numbers in the chapter <info:number>.
66
66
67
-
## BigInt
67
+
## BigInt[#bigint-type]
68
68
69
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.
Copy file name to clipboardExpand all lines: 1-js/02-first-steps/15-function-basics/article.md
+34-21Lines changed: 34 additions & 21 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -20,10 +20,10 @@ function showMessage() {
20
20
}
21
21
```
22
22
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.
When the function is called in lines `(*)` and `(**)`, the given values are copied to local variables `from` and `text`. Then the function uses them.
156
154
157
155
Here'sonemoreexample:wehaveavariable`from`andpassittothefunction. Please note: the function changes `from`, but the change is not seen outside, because a function always gets a copy of the value:
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
+
178
187
## Default values
179
188
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`.
181
190
182
191
For instance, the aforementioned function `showMessage(from, text)` can be called with a single argument:
183
192
184
193
```js
185
194
showMessage("Ann");
186
195
```
187
196
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`.
189
198
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`=`:
191
200
192
201
```jsrun
193
202
functionshowMessage(from, *!*text="no text given"*/!*) {
@@ -211,19 +220,23 @@ function showMessage(from, text = anotherFunction()) {
211
220
```smartheader="Evaluation of default parameters"
212
221
InJavaScript, adefaultparameterisevaluatedeverytimethefunction is called without the respective parameter.
213
222
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.
215
226
```
216
227
217
228
### Alternative default parameters
218
229
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.
220
231
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`:
222
233
223
234
```js run
224
235
function showMessage(text) {
236
+
// ...
237
+
225
238
*!*
226
-
if (text ===undefined) {
239
+
if (text ===undefined) {// if the parameter is missing
227
240
text ='empty message';
228
241
}
229
242
*/!*
@@ -234,21 +247,21 @@ function showMessage(text) {
234
247
showMessage(); // empty message
235
248
```
236
249
237
-
...Or we could use the `||` operator:
250
+
...Or we could use the `??` operator:
238
251
239
252
```js
240
-
// if text parameter is omitted or "" is passed, set it to 'empty'
241
253
functionshowMessage(text) {
254
+
// if text is undefined or otherwise falsy, set it to 'empty'
242
255
text = text ||'empty';
243
256
...
244
257
}
245
258
```
246
259
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":
248
261
249
262
```js run
250
-
// if there's no "count" parameter, show "unknown"
251
263
functionshowCount(count) {
264
+
// if count is undefined or null, show "unknown"
252
265
alert(count ??"unknown");
253
266
}
254
267
@@ -411,7 +424,7 @@ Functions that are used *very often* sometimes have ultrashort names.
411
424
412
425
For example, the [jQuery](http://jquery.com) framework defines a function with `$`. The [Lodash](http://lodash.com/) library has its core function named `_`.
413
426
414
-
These are exceptions. Generally functions names should be concise and descriptive.
427
+
These are exceptions. Generally function names should be concise and descriptive.
0 commit comments