5

The JSLint Error Explanations document (https://jslinterrors.com/unexpected-assignment-expression) says that if we evaluate an assignment expression (x = 0) the IF body will never be executed (y = 1).

var x, y;
if (x = 0) {
    y = 1;
}

"The body of the if statement will not be executed because the assignment expression results in undefined which is falsy."

But I could prove the opposite in if (x = 1) { y = 1; }. Is it a matter of JS version?


UPDATE

In fact I made a mistake assuming that is possible to use the result of an assignment as a test value. What I really could prove in the console was the assignment returning a value, as in x = 1 outputting 1.

2
  • 1
    Yes, this is wrong. This assignment expression does not result in undefined. Consider leaving a comment on that page. )) Commented Dec 16, 2014 at 17:25
  • 2
    LOL, we both commented on the article. :-) Commented Dec 16, 2014 at 17:56

1 Answer 1

5

That article excerpt is both correct and incorrect. It's correct that the body of the if will not be entered with if (x = 0), but it's incorrect when it says why.

The incorrect part is highlighted:

Instead of checking whether the variable x has the value 0 the example above will assign the value 0 to x. The body of the if statement will not be executed because the assignment expression results in undefined which is falsy (sic).

That's just plain wrong. The result of an assignment expression is the value that was assigned, not undefined, so if (x = 0) won't enter the body of the if (because 0 is falsey), but if (x = 1) will (because 1 is truthy).

Is it a matter of JS version?

No, it's always been this way. Someone just got it wrong in those docs.

In fact I made a mistake assuming that is possible to use the result of an assignment as a test value.

It wasn't a mistake, you can — and sometimes, you want to. This is fairly common, for instance:

while ((thing = getMeTheNextThing()) != null) {
    // ...do something with `thing`
}

That's also using the result of an assignment as a test. Sometimes people even write it like this:

while (thing = getMeTheNextThing()) {
    // ...do something with `thing`
}

...although I never do, because it's just too easy to see that as an == and mess something up.


Note that it's very, very rare to want to assign to a variable within an if statement. It does happen, but it's really rare, and most style guides will suggest you break up the assignment and the branch.

Almost always, when you see if (x = 0), what the author meant to write was if (x == 0) or if (x === 0). That is, they meant to test the value of x, not assign a new value to x.

Example of what the JSLint docs mean about assigning zero:

var x;

snippet.log("first test: using 0");
if (x = 0) {
  snippet.log("entered if body");
} else {
  snippet.log("entered else body");
}

snippet.log("second test: using 1");
if (x = 1) {
  snippet.log("entered if body");
} else {
  snippet.log("entered else body");
}
<!-- Script provides the `snippet` object, see http://meta.stackexchange.com/a/242144/134069 -->
<script src="http://tjcrowder.github.io/simple-snippets-console/snippet.js"></script>

Sign up to request clarification or add additional context in comments.

1 Comment

@Crowder, you got the point and your answer was very clarifying! Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.