Skip to content

Commit dd70a07

Browse files
authored
Minor grammar changes to help improve flow.
1 parent 3b25bc5 commit dd70a07

File tree

1 file changed

+29
-29
lines changed
  • 1-js/02-first-steps/15-function-expressions-arrows

1 file changed

+29
-29
lines changed

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

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
# Function expressions and arrows
22

3-
In JavaScript a function is not a "magical language structure", but a special kind of value.
3+
In JavaScript, a function is not a "magical language structure", but a special kind of value.
44

55
[cut]
66

7-
The syntax that we used before is called *Function Declaration*:
7+
The syntax that we used before is called a *Function Declaration*:
88

99
```js
1010
function sayHi() {
1111
alert( "Hello" );
1212
}
1313
```
1414

15-
There is another syntax of creating a function that is called *Function Expression*.
15+
There is another syntax for creating a function that is called a *Function Expression*.
1616

1717
It looks like this:
1818

@@ -22,7 +22,7 @@ let sayHi = function() {
2222
};
2323
```
2424

25-
Here the function is created and assigned to the variable explicitly, like any other value. No matter how the function is defined, it's just a value stored in the variable `sayHi`.
25+
Here, the function is created and assigned to the variable explicitly, like any other value. No matter how the function is defined, it's just a value stored in the variable `sayHi`.
2626

2727

2828
The meaning of these code samples is the same: "create a function and put it into the variable `sayHi`".
@@ -41,7 +41,7 @@ alert( sayHi ); // shows the function code
4141

4242
Please note that the last line does not run the function, because there are no parentheses after `sayHi`. There are programming languages where any mention of a function name causes its execution, but JavaScript is not like that.
4343

44-
In JavaScript, a function is a value and we can deal with it as a value. The code above shows its string representation, that is the source code.
44+
In JavaScript, a function is a value, so we can deal with it as a value. The code above shows its string representation, which is the source code.
4545

4646
It is a special value of course, in the sense that we can call it like `sayHi()`.
4747

@@ -62,13 +62,13 @@ sayHi(); // Hello // this still works too (why wouldn't it)
6262

6363
Here's what happens above in detail:
6464

65-
1. Function Declaration `(1)` creates the function and puts it into the variable named `sayHi`.
66-
2. Line `(2)` copies it into variable `func`.
65+
1. The Function Declaration `(1)` creates the function and puts it into the variable named `sayHi`.
66+
2. Line `(2)` copies it into the variable `func`.
6767

68-
Please note again: there are no parentheses after `sayHi`. If they were, then `func = sayHi()` would write *the result of the call* `sayHi()` into `func`, not *the function* `sayHi` itself.
69-
3. Now the function can be called both as `sayHi()` and `func()`.
68+
Please note again: there are no parentheses after `sayHi`. If there were, then `func = sayHi()` would write *the result of the call* `sayHi()` into `func`, not *the function* `sayHi` itself.
69+
3. Now the function can be called as both `sayHi()` and `func()`.
7070

71-
Note, that we could also have used a Function Expression to declare `sayHi`, in the first line:
71+
Note that we could also have used a Function Expression to declare `sayHi`, in the first line:
7272

7373
```js
7474
let sayHi = function() { ... };
@@ -100,7 +100,7 @@ The answer is simple:
100100

101101
## Callback functions
102102

103-
Let's see more examples of passing functions as values and using function expressions.
103+
Let's look at more examples of passing functions as values and using function expressions.
104104

105105
We'll write a function `ask(question, yes, no)` with three parameters:
106106

@@ -137,13 +137,13 @@ ask("Do you agree?", showOk, showCancel);
137137

138138
Before we explore how we can write it in a much shorter way, let's note that in the browser (and on the server-side in some cases) such functions are quite popular.
139139

140-
The major difference between a real-life implementation 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 a function usually draws a nice-looking question window. But that's another story.
140+
The major difference between a real-life implementation 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 a function usually draws a nice-looking question window. But that's another story.
141141

142142
**The arguments of `ask` are called *callback functions* or just *callbacks*. The idea is that we pass a function and expect it to be "called back" in certain circumstances.**
143143

144144
So, `showOk` becomes the callback for the "yes" answer, and `showCancel` for the "no" answer.
145145

146-
We can use Function Expressions to write the same much shorter:
146+
We can use Function Expressions to write the same function much shorter:
147147

148148
```js run no-beautify
149149
function ask(question, yes, no) {
@@ -160,7 +160,7 @@ ask(
160160
*/!*
161161
```
162162

163-
Here functions are declared right inside the `ask(...)` call. They have no name, and so are called *anonymous*. Such functions are not accessible outside of `ask` (because they are not assigned to variables), but that's just what we want here.
163+
Here, functions are declared right inside the `ask(...)` call. They have no name, and so are called *anonymous*. Such functions are not accessible outside of `ask` (because they are not assigned to variables), but that's just what we want here.
164164

165165
Such code appears in our scripts very naturally, it's in the spirit of JavaScript.
166166

@@ -190,7 +190,7 @@ First, the syntax: how to see what is what in the code.
190190
```
191191
- *Function Expression:* a function, created inside an expression or inside another syntax construct.
192192

193-
Here the function is created at the right side of the "assignment expression =":
193+
Here, the function is created at the right side of the "assignment expression =":
194194
```js
195195
// Function Expression
196196
let sum = function(a, b) {
@@ -200,19 +200,19 @@ First, the syntax: how to see what is what in the code.
200200

201201
The more subtle difference is *when* a function is created by the JavaScript engine.
202202

203-
**Function Expression is created when the execution reaches it and is usable from then on.**
203+
**A Function Expression is created when the execution reaches it and is usable from then on.**
204204

205205
Once the execution flow passes to the right side of the assignment `let sum = function…` -- here we go, the function is created and can be used (assigned, called etc) from now on.
206206

207207
Function Declarations are different.
208208

209-
**Function Declaration is usable in the whole script/code block.**
209+
**A Function Declaration is usable in the whole script/code block.**
210210

211211
In other words, when JavaScript *prepares* to run the script or a code block, it first looks for Function Declarations in it and creates the functions. We can think of it as an "initialization stage".
212212

213-
And after all Function Declarations are processed, the execution goes on.
213+
And after all of the Function Declarations are processed, the execution goes on.
214214

215-
As a natural effect, a function declared as Function Declaration can be called earlier than it is defined.
215+
As a result, a function declared as a Function Declaration can be called earlier than it is defined.
216216

217217
For example, this works:
218218

@@ -228,7 +228,7 @@ function sayHi(name) {
228228

229229
The Function Declaration `sayHi` is created when JavaScript is preparing to start the script and is visible everywhere in it.
230230

231-
...And if there were Function Expression, then it wouldn't work:
231+
...If it was a Function Expression, then it wouldn't work:
232232

233233
```js run refresh untrusted
234234
*!*
@@ -275,7 +275,7 @@ welcome(); // Error: welcome is not defined
275275
*/!*
276276
```
277277

278-
A Function Declaration is visible only inside the code block where it resides.
278+
A Function Declaration is only visible inside the code block in which it resides.
279279

280280
We can call it from within the block, but not from outside:
281281

@@ -313,7 +313,7 @@ welcome(); // Error: welcome is not defined
313313

314314
What can we do to make `welcome` visible outside of `if`?
315315

316-
The right thing would be to use a Function Expression and assign `welcome` to the variable which is declared outside of `if` and has the proper visibility:
316+
The correct approach would be to use a Function Expression and assign `welcome` to the variable that is declared outside of `if` and has the proper visibility:
317317

318318
```js run
319319
let age = prompt("What is your age?", 18);
@@ -359,13 +359,13 @@ As a rule of thumb, when we need to declare a function, the first to consider is
359359
360360
It's also a little bit easier to look up `function f(…) {…}` in the code than `let f = function(…) {…}`. Function Declarations are more "eye-catching".
361361
362-
...But if Function Declaration does not suit us for some reason (we've seen an example above), then Function Expression should be used.
362+
...But if a Function Declaration does not suit us for some reason (we've seen an example above), then Function Expression should be used.
363363
```
364364

365365

366366
## Arrow functions [#arrow-functions]
367367

368-
There's one more syntax for creating functions -- very simple and concise. It's called "arrow functions", because it looks like this:
368+
There's one more very simple and concise syntax for creating functions. It's called "arrow functions", because it looks like this:
369369

370370

371371
```js
@@ -384,7 +384,7 @@ let func = function(arg1, arg2, ...argN) {
384384

385385
...But much shorter.
386386

387-
Let's see the example:
387+
Let's see an example:
388388

389389
```js run
390390
let sum = (a, b) => a + b;
@@ -412,15 +412,15 @@ let double = n => n*2;
412412
alert( double(3) ); // 6
413413
```
414414

415-
If there are no arguments, we can put empty parentheses:
415+
If there are no arguments, we can insert empty parentheses:
416416

417417
```js run
418418
let sayHi = () => alert("Hello!");
419419

420420
sayHi();
421421
```
422422

423-
Arrow functions can be used the same way as Function Expressions.
423+
Arrow functions can be used in the same way as Function Expressions.
424424

425425
For instance, here's the rewritten example with `welcome()`:
426426

@@ -472,9 +472,9 @@ For now, we can already use them for one-line actions and callbacks.
472472
- Function Expressions are created when the execution flow reaches them.
473473

474474

475-
In most cases when we need to declare a function, Function Declaration is preferable, because it is visible prior to the declaration itself. That gives us more flexibility in code organization. And is usually more readable.
475+
In most cases when we need to declare a function, a Function Declaration is preferable, because it is visible prior to the declaration itself. That gives us more flexibility in code organization, and is usually more readable.
476476

477-
So we should use Function Expression only when Function Declaration does not fit the task. We've seen a couple of examples of that in the chapter, and will see more in the future.
477+
So we should use a Function Expression only when a Function Declaration is not fit for the task. We've seen a couple of examples of that in this chapter, and will see more in the future.
478478

479479
Arrow functions are handy for one-liners. They come in two flavors:
480480

0 commit comments

Comments
 (0)