Skip to content

Commit 64da7ff

Browse files
committed
minor fixes
1 parent a829155 commit 64da7ff

File tree

1 file changed

+20
-8
lines changed
  • 1-js/02-first-steps/16-function-expressions

1 file changed

+20
-8
lines changed

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

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,29 @@ function sayHi() {
1212

1313
There is another syntax for creating a function that is called a *Function Expression*.
1414

15-
It looks like this:
15+
It allows to create a new function in the middle of any expression.
16+
17+
For example:
1618

1719
```js
1820
let sayHi = function() {
1921
alert( "Hello" );
2022
};
2123
```
2224

23-
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 we can see a variable `sayHi` getting a value, the new function, created as `function() { alert("Hello"); }`.
26+
27+
As the function creation happens in the context of the assignment expression (to the right side of `=`), this is a *Function Expression*.
28+
29+
Please note, there's no name after the `function` keyword. Omitting a name is allowed for Function Expressions.
30+
31+
Here we immediately assign it to the variable, so the meaning of these code samples is the same: "create a function and put it into the variable `sayHi`".
2432

25-
The meaning of these code samples is the same: "create a function and put it into the variable `sayHi`".
33+
In more advanced situations, that we'll come across later, a function may be created and immediately called or scheduled for a later execution, not stored anywhere, thus remaining anonymous.
34+
35+
## Function is a value
36+
37+
Let's reiterate: no matter how the function is created, a function is a value. Both examples above store a function is `sayHi` variable.
2638

2739
We can even print out that value using `alert`:
2840

@@ -63,10 +75,10 @@ Here's what happens above in detail:
6375
2. Line `(2)` copies it into the variable `func`. 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.
6476
3. Now the function can be called as both `sayHi()` and `func()`.
6577

66-
Note that we could also have used a Function Expression to declare `sayHi`, in the first line:
78+
We could also have used a Function Expression to declare `sayHi`, in the first line:
6779

6880
```js
69-
let sayHi = function() {
81+
let sayHi = function() { // (1) create
7082
alert( "Hello" );
7183
};
7284

@@ -90,9 +102,9 @@ let sayHi = function() {
90102
}*!*;*/!*
91103
```
92104
93-
The answer is simple:
94-
- There's no need for `;` at the end of code blocks and syntax structures that use them like `if { ... }`, `for { }`, `function f { }` etc.
95-
- A Function Expression is used inside the statement: `let sayHi = ...;`, as a value. It's not a code block, but rather an assignment. The semicolon `;` is recommended at the end of statements, no matter what the value is. So the semicolon here is not related to the Function Expression itself, it just terminates the statement.
105+
The answer is simple: a Function Expression is created here as `function(…) {…}` inside the assignment statement: `let sayHi = …;`. The semicolon `;` is recommended at the end of the statement, it's not a part of the function syntax.
106+
107+
The semicolon would be there for a simpler assignment, such as `let sayHi = 5;`, and it's also there for a function assignment.
96108
````
97109

98110
## Callback functions

0 commit comments

Comments
 (0)