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/14-function-basics/article.md
+7-7Lines changed: 7 additions & 7 deletions
Original file line number
Diff line number
Diff line change
@@ -20,7 +20,7 @@ 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*in the brackets (empty in the example above) and finally the code of the function, also named "the function body".
23
+
The `function` keyword goes first, then goes the *name of the function*, then a list of *parameters*between the parentheses (empty in the example above) and finally the code of the function, also named "the function body", between curly braces.
24
24
25
25

26
26
@@ -41,7 +41,7 @@ showMessage();
41
41
42
42
The call `showMessage()` executes the code of the function. Here we will see the message two times.
43
43
44
-
This example clearly demonstrates one of the main purposes of functions: to evade code duplication.
44
+
This example clearly demonstrates one of the main purposes of functions: to avoid code duplication.
45
45
46
46
If we ever need to change the message or the way it is shown, it's enough to modify the code in one place: the function which outputs it.
47
47
@@ -117,7 +117,7 @@ function showMessage() {
117
117
alert(message);
118
118
}
119
119
120
-
// the function will create and use it's own userName
120
+
// the function will create and use its own userName
121
121
showMessage();
122
122
123
123
alert( userName ); // *!*John*/!*, unchanged, the function did not access the outer variable
@@ -128,7 +128,7 @@ Variables declared outside of any function, such as the outer `userName` in the
128
128
129
129
Global variables are visible from any function (unless shadowed by locals).
130
130
131
-
Usually, a function declares all variables specific to its task, and global variables only store project-level data, so important that it really must be seen from anywhere. Modern code has few or no globals. Most variables reside in their functions.
131
+
Usually, a function declares all variables specific to its task. Global variables only store project-level data, so when it's important that these variables are accesible from anywhere. Modern code has few or no globals. Most variables reside in their functions.
132
132
```
133
133
134
134
## Parameters
@@ -376,7 +376,7 @@ These examples assume common meanings of prefixes. What they mean for you is det
376
376
```smart header="Ultrashort function names"
377
377
Functions that are used *very often* sometimes have ultrashort names.
378
378
379
-
For example, [jQuery](http://jquery.com) framework defines a function `$`, [LoDash](http://lodash.com/) library has it's core function named `_`.
379
+
For example, the [jQuery](http://jquery.com) framework defines a function `$`. The [LoDash](http://lodash.com/) library has its core function named `_`.
380
380
381
381
These are exceptions. Generally functions names should be concise, but descriptive.
382
382
```
@@ -424,7 +424,7 @@ function isPrime(n) {
424
424
}
425
425
```
426
426
427
-
The second variant is easier to understand isn't it? Instead of the code piece we see a name of the action (`isPrime`). Sometimes people refer to such code as *self-describing*.
427
+
The second variant is easier to understand, isn't it? Instead of the code piece we see a name of the action (`isPrime`). Sometimes people refer to such code as *self-describing*.
428
428
429
429
So, functions can be created even if we don't intend to reuse them. They structure the code and make it readable.
430
430
@@ -440,7 +440,7 @@ function name(parameters, delimited, by, comma) {
440
440
441
441
- Values passed to a function as parameters are copied to its local variables.
442
442
- A function may access outer variables. But it works only from inside out. The code outside of the function doesn't see its local variables.
443
-
- A function can return a value. If it doesn't then its result is `undefined`.
443
+
- A function can return a value. If it doesn't, then its result is `undefined`.
444
444
445
445
To make the code clean and easy to understand, it's recommended to use mainly local variables and parameters in the function, not outer variables.
0 commit comments