1 # require `return` statements to either always or never specify values (consistent-return)
3 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.
5 A confusing aspect of JavaScript is that a function returns `undefined` if any of the following are true:
7 * it does not execute a `return` statement before it exits
8 * it executes `return` which does not specify a value explicitly
9 * it executes `return undefined`
10 * it executes `return void` followed by an expression (for example, a function call)
11 * it executes `return` followed by any other expression which evaluates to `undefined`
13 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:
15 * a code path through the function returns a Boolean value `true`
16 * another code path does not return a value explicitly, therefore returns `undefined` implicitly
19 function doSomething(condition) {
30 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.
32 Examples of **incorrect** code for this rule:
35 /*eslint consistent-return: "error"*/
37 function doSomething(condition) {
45 function doSomething(condition) {
52 Examples of **correct** code for this rule:
55 /*eslint consistent-return: "error"*/
57 function doSomething(condition) {
66 if (!(this instanceof Foo)) {
76 This rule has an object option:
78 * `"treatUndefinedAsUnspecified": false` (default) always either specify values or return `undefined` implicitly only.
79 * `"treatUndefinedAsUnspecified": true` always either specify values or return `undefined` explicitly or implicitly.
81 ### treatUndefinedAsUnspecified
83 Examples of **incorrect** code for this rule with the default `{ "treatUndefinedAsUnspecified": false }` option:
86 /*eslint consistent-return: ["error", { "treatUndefinedAsUnspecified": false }]*/
88 function foo(callback) {
90 return void callback();
92 // no return statement
95 function bar(condition) {
99 // no return statement
103 Examples of **incorrect** code for this rule with the `{ "treatUndefinedAsUnspecified": true }` option:
106 /*eslint consistent-return: ["error", { "treatUndefinedAsUnspecified": true }]*/
108 function foo(callback) {
110 return void callback();
115 function bar(condition) {
123 Examples of **correct** code for this rule with the `{ "treatUndefinedAsUnspecified": true }` option:
126 /*eslint consistent-return: ["error", { "treatUndefinedAsUnspecified": true }]*/
128 function foo(callback) {
130 return void callback();
132 // no return statement
135 function bar(condition) {
139 // no return statement
143 ## When Not To Use It
145 If you want to allow functions to have different `return` behavior depending on code branching, then it is safe to disable this rule.