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
There are two ways of defining functions in JavaScript: `function` declarations and `function` expressions. Declarations contain the `function` keyword first, followed by a name and then its arguments and the function body, for example:
8
+
There are two ways of defining functions in JavaScript: `function` declarations and function expressions assigned to variables. Function declarations are statements that begin with the `function` keyword. Function expressions can either be arrow functions or use the `function` keyword with an optional name. Here are some examples:
10
9
11
10
```js
11
+
// function declaration
12
12
functiondoSomething() {
13
13
// ...
14
14
}
15
-
```
16
15
17
-
Equivalent function expressions begin with the `var` keyword, followed by a name and then the function itself, such as:
16
+
// arrow function expression assigned to a variable
17
+
constdoSomethingElse= () => {
18
+
// ...
19
+
};
18
20
19
-
```js
20
-
vardoSomething=function() {
21
+
// function expression assigned to a variable
22
+
constdoSomethingAgain=function() {
21
23
// ...
22
24
};
23
25
```
24
26
25
-
The primary difference between `function` declarations and `function expressions` is that declarations are *hoisted* to the top of the scope in which they are defined, which allows you to write code that uses the function before its declaration. For example:
27
+
The primary difference between `function` declarations and function expressions is that declarations are *hoisted* to the top of the scope in which they are defined, which allows you to write code that uses the function before its declaration. For example:
26
28
27
29
```js
28
-
doSomething();
30
+
doSomething();// ok
29
31
30
32
functiondoSomething() {
31
33
// ...
32
34
}
33
35
```
34
36
35
-
Although this code might seem like an error, it actually works fine because JavaScript engines hoist the `function` declarations to the top of the scope. That means this code is treated as if the declaration came before the invocation.
36
-
37
-
For `function` expressions, you must define the function before it is used, otherwise it causes an error. Example:
37
+
For function expressions, you must define the function before it is used, otherwise it causes an error. Example:
38
38
39
39
```js
40
40
doSomething(); // error!
@@ -50,7 +50,9 @@ Due to these different behaviors, it is common to have guidelines as to which st
50
50
51
51
## Rule Details
52
52
53
-
This rule enforces a particular type of `function` style throughout a JavaScript file, either declarations or expressions. You can specify which you prefer in the configuration.
53
+
This rule enforces a particular type of function style, either `function` declarations or expressions assigned to variables. You can specify which you prefer in the configuration.
54
+
55
+
Note: This rule does not apply to *all* functions. For example, a callback function passed as an argument to another function is not considered by this rule.
0 commit comments