Enforce Function Style (func-style)

There are two ways of defining functions in JavaScript: function declarations and function expressions. Declarations have the function keyword first, followed by a name, followed by its arguments and the function body, such as:

function doSomething() {
    // ...
}

Equivalent function expressions begin with the var keyword, followed by a name, and then the function itself, such as:

var doSomething = function() {
    // ...
};

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 the declaration. For example:

doSomething();

function doSomething() {
    // ...
}

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.

For function expressions, you must define the function before it is used, otherwise it causes an error. Example:

doSomething();  // error!

var doSomething = function() {
    // ...
};

In this case, doSomething() is undefined at the time of invocation and so causes a runtime error.

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.

Rule Details

This rule is aimed at enforcing a particular type of function style throughout a JavaScript file, either declarations or expressions. You can specify which you prefer in the configuration.

Options

"expression"

This is the default configuration. It reports an error when function declarations are used instead of function expressions.

"func-style": [2, "expression"]

An additional option object can be added with a property "allowArrowFunctions". Setting this to true will allow arrow functions.

"func-style": [2, "expression", { "allowArrowFunctions": true }]

"declaration"

This reports an error if any function expressions are used where function declarations are expected. You can specify to use expressions instead:

"func-style": [2, "declaration"]

Examples

The following patterns are considered problems:

/*eslint func-style: [2, "declaration"]*/

var foo = function() {  /*error Expected a function declaration.*/
    // ...
};
/*eslint func-style: [2, "expression"]*/

function foo() {  /*error Expected a function expression.*/
    // ...
}
/*eslint func-style: [2, "declaration"]*/

var foo = () => {};  /*error Expected a function declaration.*/

The following patterns are not considered problems:

/*eslint func-style: [2, "declaration"]*/

function foo() {
    // ...
}

// Methods (functions assigned to objects) are not checked by this rule
SomeObject.foo = function() {
    // ...
};
/*eslint func-style: [2, "expression"]*/

var foo = function() {
    // ...
};
/*eslint func-style: [2, "declaration", { "allowArrowFunctions": true }]*/

var foo = () => {};

When Not To Use It

If you want to allow developers to each decide how they want to write functions on their own, then you can disable this rule.

Further Reading

Version

This rule was introduced in ESLint 0.2.0.

Resources