1 # enforce the consistent use of either `function` declarations or expressions (func-style)
3 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:
6 function doSomething() {
11 Equivalent function expressions begin with the `var` keyword, followed by a name and then the function itself, such as:
14 var doSomething = function() {
19 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:
24 function doSomething() {
29 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.
31 For `function` expressions, you must define the function before it is used, otherwise it causes an error. Example:
34 doSomething(); // error!
36 var doSomething = function() {
41 In this case, `doSomething()` is undefined at the time of invocation and so causes a runtime error.
43 Due to these different behaviors, it is common to have guidelines as to which style of function should be used. There is really no correct or incorrect choice here, it is just a preference.
47 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.
51 This rule has a string option:
53 * `"expression"` (default) requires the use of function expressions instead of function declarations
54 * `"declaration"` requires the use of function declarations instead of function expressions
56 This rule has an object option for an exception:
58 * `"allowArrowFunctions"`: `true` (default `false`) allows the use of arrow functions. This option applies only when the string option is set to `"declaration"` (arrow functions are always allowed when the string option is set to `"expression"`, regardless of this option)
62 Examples of **incorrect** code for this rule with the default `"expression"` option:
65 /*eslint func-style: ["error", "expression"]*/
72 Examples of **correct** code for this rule with the default `"expression"` option:
75 /*eslint func-style: ["error", "expression"]*/
77 var foo = function() {
83 // allowed as allowArrowFunctions : false is applied only for declaration
88 Examples of **incorrect** code for this rule with the `"declaration"` option:
91 /*eslint func-style: ["error", "declaration"]*/
93 var foo = function() {
100 Examples of **correct** code for this rule with the `"declaration"` option:
103 /*eslint func-style: ["error", "declaration"]*/
109 // Methods (functions assigned to objects) are not checked by this rule
110 SomeObject.foo = function() {
115 ### allowArrowFunctions
117 Examples of additional **correct** code for this rule with the `"declaration", { "allowArrowFunctions": true }` options:
120 /*eslint func-style: ["error", "declaration", { "allowArrowFunctions": true }]*/
125 ## When Not To Use It
127 If you want to allow developers to each decide how they want to write functions on their own, then you can disable this rule.
131 * [JavaScript Scoping and Hoisting](http://www.adequatelygood.com/JavaScript-Scoping-and-Hoisting.html)