2 title: consistent-return
8 Unlike statically-typed languages which enforce that a function returns a specified type of value, JavaScript allows different code paths in a function to return different types of values.
10 A confusing aspect of JavaScript is that a function returns `undefined` if any of the following are true:
12 * it does not execute a `return` statement before it exits
13 * it executes `return` which does not specify a value explicitly
14 * it executes `return undefined`
15 * it executes `return void` followed by an expression (for example, a function call)
16 * it executes `return` followed by any other expression which evaluates to `undefined`
18 If any code paths in a function return a value explicitly but some code path do not return a value explicitly, it might be a typing mistake, especially in a large function. In the following example:
20 * a code path through the function returns a Boolean value `true`
21 * another code path does not return a value explicitly, therefore returns `undefined` implicitly
24 function doSomething(condition) {
35 This rule requires `return` statements to either always or never specify values. This rule ignores function definitions where the name begins with an uppercase letter, because constructors (when invoked with the `new` operator) return the instantiated object implicitly if they do not return another object explicitly.
37 Examples of **incorrect** code for this rule:
42 /*eslint consistent-return: "error"*/
44 function doSomething(condition) {
52 function doSomething(condition) {
61 Examples of **correct** code for this rule:
66 /*eslint consistent-return: "error"*/
68 function doSomething(condition) {
77 if (!(this instanceof Foo)) {
89 This rule has an object option:
91 * `"treatUndefinedAsUnspecified": false` (default) always either specify values or return `undefined` implicitly only.
92 * `"treatUndefinedAsUnspecified": true` always either specify values or return `undefined` explicitly or implicitly.
94 ### treatUndefinedAsUnspecified
96 Examples of **incorrect** code for this rule with the default `{ "treatUndefinedAsUnspecified": false }` option:
101 /*eslint consistent-return: ["error", { "treatUndefinedAsUnspecified": false }]*/
103 function foo(callback) {
105 return void callback();
107 // no return statement
110 function bar(condition) {
114 // no return statement
120 Examples of **incorrect** code for this rule with the `{ "treatUndefinedAsUnspecified": true }` option:
125 /*eslint consistent-return: ["error", { "treatUndefinedAsUnspecified": true }]*/
127 function foo(callback) {
129 return void callback();
134 function bar(condition) {
144 Examples of **correct** code for this rule with the `{ "treatUndefinedAsUnspecified": true }` option:
149 /*eslint consistent-return: ["error", { "treatUndefinedAsUnspecified": true }]*/
151 function foo(callback) {
153 return void callback();
155 // no return statement
158 function bar(condition) {
162 // no return statement
168 ## When Not To Use It
170 If you want to allow functions to have different `return` behavior depending on code branching, then it is safe to disable this rule.