Constructors should not return values.

Why is this an issue?

JavaScript allows returning a value from a class constructor. This obscure feature is rarely used and is more likely a bug than the developer’s intention.

How to fix it

Do not return anything from the constructor.

Code examples

Noncompliant code example

class TextMessage {
    constructor(msg) {
        this.text = msg;
        return msg; // Noncompliant
    }
}

Compliant solution

class TextMsg1 {
    constructor(msg) {
        this.text = msg;
    }
}


class TextMsg2 {
    constructor(msg) {
        if (!msg) {
            return; // ok to return nothing for flow control
        }
        this.text = msg;
    }
}

Resources

Documentation