Bad assignment

History

This warning has existed in two forms across the three main linters. It was introduced in the original version of JSLint and has remained in all three tools ever since.

  • In JSLint and JSHint the warning given has always been "Bad assignment"

  • In ESLint the warning has always been "Invalid left-hand side in assignment"

The situations that produce the warning have not changed despite changes to the text of the warning itself.

When do I get this error?

The "Bad assignment" error (and the alternative "Invalid left-hand side in assignment" error) are thrown when JSLint, JSHint or ESLint encounters an assignment expression in which the left-hand side is a call expression. In the following example we have an if statement with an assignment expression where you would normally expect a conditional:

function example() {
    "use strict";
    example() = 1;
}

JSLint also raises this warning when it encounters an assignment to a property of the arguments object. In this example we attempt to define an x property:

function example() {
    "use strict";
    arguments.x = 1;
}

Why do I get this error?

In the case of assignment to a function call this error is raised to highlight a fatal reference error. Your code will throw an error in all environments if you do not resolve this issue. It makes no sense to assign a value to a call expression. The return value will be a literal value (such as a string or number) or an object reference, neither of which can be directly assigned to. Imagine it as if you're trying to do 123 = 234.

If you're receiving the warning in this situation the chances are you were actually trying to perform a comparison rather than an assignment. If that's the case just ensure you're using a comparison operator such as === instead of the assignment operator =:

function example() {
    "use strict";
    if (example() === 1) { // Comparison
        return true;
    }
}

In the case of assignment to a property of an arguments object this error is raised to highlight a bad practice. The arguments object is notoriously difficult to work with and has behaviour that differs significantly between "normal" and "strict" mode. JSLint has numerous warnings related to abuse of the arguments object but if you're receiving the "Bad assignment" error the chances are you can use a normal variable instead:

function example() {
    "use strict";
    // arguments.x = 1;
    var x = 1;
    return x;
}

In JSHint 1.0.0 and above you have the ability to ignore any warning with a special option syntax. Since this message relates to a fatal reference error you cannot disable it.

In ESLint this error is generated by the Esprima parser and can therefore not be disabled.


About the author

James Allardice

This article was written by James Allardice, Software engineer at Tesco and orangejellyfish in London. Passionate about React, Node and writing clean and maintainable JavaScript. Uses linters (currently ESLint) every day to help achieve this.