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
+20-8Lines changed: 20 additions & 8 deletions
Original file line number
Diff line number
Diff line change
@@ -12,17 +12,29 @@ function sayHi() {
12
12
13
13
There is another syntax for creating a function that is called a *Function Expression*.
14
14
15
-
It looks like this:
15
+
It allows to create a new function in the middle of any expression.
16
+
17
+
For example:
16
18
17
19
```js
18
20
letsayHi=function() {
19
21
alert( "Hello" );
20
22
};
21
23
```
22
24
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`".
24
32
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.
26
38
27
39
We can even print out that value using `alert`:
28
40
@@ -63,10 +75,10 @@ Here's what happens above in detail:
63
75
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.
64
76
3. Now the function can be called as both `sayHi()` and `func()`.
65
77
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:
67
79
68
80
```js
69
-
letsayHi=function() {
81
+
letsayHi=function() {// (1) create
70
82
alert( "Hello" );
71
83
};
72
84
@@ -90,9 +102,9 @@ let sayHi = function() {
90
102
}*!*;*/!*
91
103
```
92
104
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.
0 commit comments