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/02-first-steps/16-function-expressions/article.md
+8-8Lines changed: 8 additions & 8 deletions
Original file line number
Diff line number
Diff line change
@@ -90,7 +90,7 @@ Everything would work the same.
90
90
91
91
92
92
````smart header="Why is there a semicolon at the end?"
93
-
You might wonder, why does Function Expression have a semicolon `;` at the end, but Function Declaration does not:
93
+
You might wonder, why do Function Expressions have a semicolon `;` at the end, but Function Declarations do not:
94
94
95
95
```js
96
96
function sayHi() {
@@ -144,13 +144,13 @@ function showCancel() {
144
144
ask("Do you agree?", showOk, showCancel);
145
145
```
146
146
147
-
In practice, such functions are quite useful. The major difference between a real-life `ask` and the example above is that real-life functions use more complex ways to interact with the user than a simple `confirm`. In the browser, such function usually draws a nice-looking question window. But that's another story.
147
+
In practice, such functions are quite useful. The major difference between a real-life `ask` and the example above is that real-life functions use more complex ways to interact with the user than a simple `confirm`. In the browser, such functions usually draw a nice-looking question window. But that's another story.
148
148
149
149
**The arguments `showOk` and `showCancel` of `ask` are called *callback functions* or just *callbacks*.**
150
150
151
151
The idea is that we pass a function and expect it to be "called back" later if necessary. In our case, `showOk` becomes the callback for "yes" answer, and `showCancel` for "no" answer.
152
152
153
-
We can use Function Expressions to write the same function much shorter:
153
+
We can use Function Expressions to write an equivalent, shorter function:
154
154
155
155
```js run no-beautify
156
156
functionask(question, yes, no) {
@@ -186,15 +186,15 @@ Let's formulate the key differences between Function Declarations and Expression
186
186
187
187
First, the syntax: how to differentiate between them in the code.
188
188
189
-
-*Function Declaration:* a function, declared as a separate statement, in the main code flow.
189
+
-*Function Declaration:* a function, declared as a separate statement, in the main code flow:
190
190
191
191
```js
192
192
// Function Declaration
193
193
functionsum(a, b) {
194
194
return a + b;
195
195
}
196
196
```
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" `=`:
198
198
199
199
```js
200
200
// Function Expression
@@ -291,7 +291,7 @@ if (age < 18) {
291
291
welcome(); // \ (runs)
292
292
*/!*
293
293
// |
294
-
functionwelcome() { // |
294
+
functionwelcome() { // |
295
295
alert("Hello!"); // | Function Declaration is available
296
296
} // | everywhere in the block where it's declared
297
297
// |
@@ -301,7 +301,7 @@ if (age < 18) {
301
301
302
302
} else {
303
303
304
-
functionwelcome() {
304
+
functionwelcome() {
305
305
alert("Greetings!");
306
306
}
307
307
}
@@ -360,7 +360,7 @@ welcome(); // ok now
360
360
361
361
362
362
```smart header="When to choose Function Declaration versus Function Expression?"
363
-
As a rule of thumb, when we need to declare a function, the first to consider is Function Declaration syntax. It gives more freedom in how to organize our code, because we can call such functions before they are declared.
363
+
As a rule of thumb, when we need to declare a function, the first thing to consider is Function Declaration syntax. It gives more freedom in how to organize our code, because we can call such functions before they are declared.
364
364
365
365
That's also better for readability, as it's easier to look up `function f(…) {…}` in the code than `let f = function(…) {…};`. Function Declarations are more "eye-catching".
Copy file name to clipboardExpand all lines: 1-js/02-first-steps/17-arrow-functions-basics/article.md
+6-6Lines changed: 6 additions & 6 deletions
Original file line number
Diff line number
Diff line change
@@ -48,7 +48,7 @@ As you can see, `(a, b) => a + b` means a function that accepts two arguments na
48
48
alert( double(3) ); // 6
49
49
```
50
50
51
-
- If there are no arguments, parentheses will be empty (but they should be present):
51
+
- If there are no arguments, parentheses are empty, but they must be present:
52
52
53
53
```js run
54
54
let sayHi = () => alert("Hello!");
@@ -64,7 +64,7 @@ For instance, to dynamically create a function:
64
64
let age = prompt("What is your age?", 18);
65
65
66
66
let welcome = (age < 18) ?
67
-
() => alert('Hello') :
67
+
() => alert('Hello!') :
68
68
() => alert("Greetings!");
69
69
70
70
welcome();
@@ -76,9 +76,9 @@ They are very convenient for simple one-line actions, when we're just too lazy t
76
76
77
77
## Multiline arrow functions
78
78
79
-
The examples above took arguments from the left of `=>`and evaluated the right-side expression with them.
79
+
The arrow functions that we've seen so far were very simple. Theytook arguments from the left of`=>`, evaluated and returned the right-side expression with them.
80
80
81
-
Sometimes we need something a little bit more complex, like multiple expressions or statements. It is also possible, but we should enclose them in curly braces. Then use a normal `return` within them.
81
+
Sometimes we need a more complexfunction, with multiple expressions and statements. In that case, we can enclose them in curly braces. The major difference is that curly braces require a `return` within them to return a value (justlikearegularfunction does).
82
82
83
83
Like this:
84
84
@@ -105,7 +105,7 @@ For now, we can already use arrow functions for one-line actions and callbacks.
105
105
106
106
## Summary
107
107
108
-
Arrow functions are handy for one-liners. They come in two flavors:
108
+
Arrow functions are handy for simple actions, especially for one-liners. They come in two flavors:
109
109
110
-
1. Without curly braces: `(...args) => expression` -- the right side is an expression: the function evaluates it and returns the result.
110
+
1. Without curly braces: `(...args) =>expression` -- the right side is an expression: the function evaluates it and returns the result. Parentheses can be omitted, if there's only a single argument, e.g. `n=>n*2`.
111
111
2. With curly braces: `(...args) => { body }` -- brackets allow us to write multiple statements inside the function, but we need an explicit `return` to return something.
Copy file name to clipboardExpand all lines: 1-js/02-first-steps/18-javascript-specials/article.md
+3-3Lines changed: 3 additions & 3 deletions
Original file line number
Diff line number
Diff line change
@@ -55,7 +55,7 @@ To fully enable all features of modern JavaScript, we should start scripts with
55
55
56
56
The directive must be at the top of a script or at the beginning of a function body.
57
57
58
-
Without `"use strict"`, everything still works, but some features behave in the old-fashion, "compatible" way. We'd generally prefer the modern behavior.
58
+
Without `"use strict"`, everything still works, but some features behave in the old-fashioned, "compatible" way. We'd generally prefer the modern behavior.
59
59
60
60
Some modern features of the language (like classes that we'll study in the future) enable strict mode implicitly.
61
61
@@ -144,7 +144,7 @@ Assignments
144
144
: There is a simple assignment: `a = b` and combined ones like `a *= 2`.
145
145
146
146
Bitwise
147
-
: Bitwise operators work with 32-bit integers at the lowest, bit-level: see the [docs](mdn:/JavaScript/Guide/Expressions_and_Operators#Bitwise) when they are needed.
147
+
: Bitwise operators work with 32-bit integers at the lowest, bit-level: see the [docs](mdn:/JavaScript/Guide/Expressions_and_Operators#bitwise_operators) when they are needed.
148
148
149
149
Conditional
150
150
: The only operator with three parameters: `cond ? resultA : resultB`. If `cond` is truthy, returns `resultA`, otherwise `resultB`.
@@ -256,7 +256,7 @@ We covered three ways to create a function in JavaScript:
256
256
3. Arrow functions:
257
257
258
258
```js
259
-
// expression at the right side
259
+
// expression on the right side
260
260
let sum = (a, b) => a + b;
261
261
262
262
// or multi-line syntax with { ... }, need return here:
0 commit comments