no-constructor-return
Returning a value in a class constructor may be a mistake resulting from unfamiliarity with JavaScript or a copy-paste error.
Rule Type: Problem
Fixable: No
Why This Rule Exists
In JavaScript, returning a value from a constructor is unusual and often indicates a mistake. While JavaScript does allow it, the behavior can be confusing:
- Returning an object replaces the instance being constructed
- Returning a primitive value is ignored
- An empty
return statement is valid for early exit
Most developers don’t intend to return values from constructors, so this rule catches potential bugs.
Rule Details
This rule disallows return statements in class constructors that return a value. Empty return statements (for control flow) are allowed.
Examples
Incorrect Code
Correct Code
Common Mistakes
Copy-Paste from Regular Functions
Returning ‘this’ Unnecessarily
Validation in Constructor
Understanding Constructor Return Behavior
JavaScript’s constructor return behavior is complex and often surprising.
Alternatives
Factory Functions
If you need conditional object creation, use a factory function:
Throwing Errors
For validation, throw errors in the constructor:
When Not to Use It
This rule should almost never be disabled. If you find yourself needing to return values from constructors, consider refactoring to use factory functions or static methods instead.