2

Whenever I do something like this...

var obj;

while (obj = doSomething()) {
  // something with obj
}

JSHint tells me warning 84| Expected a conditional expression and instead saw an assignment.. However, doing obj = doSomething() returns the value that doSomething() returns during assignment, so it makes sense to write a while loop in this fashion.

Is there a specific reason that JSHint warns me, and more importantly, are there reasons to not do this? Or can I tell JSHint to ignore those lines for that specific warning?

josh
  • 9,656
  • 4
  • 34
  • 51
  • You can also use `for (var obj = doSomething(); obj; obj = doSomething())` – Bergi Dec 12 '14 at 07:45
  • @Bergi At first glance that looks like quite a wacky for-loop, using `while` seems easier to read to me. – Spencer Wieczorek Dec 12 '14 at 07:49
  • See also http://stackoverflow.com/questions/8108184/why-does-jshint-not-recognize-an-assignment-as-an-expression. By the way, this is not a "while block *declaration*", but rather a "while [block] *condition*". –  Dec 12 '14 at 08:06
  • @Bergi That's pretty interesting -- wouldn't have thought to use a for loop in that way though. – josh Dec 12 '14 at 09:29
  • @torazaburo I don't think you understood my question when you say it's as a duplicate of that question, because it and this question are discussing similar but distinct things. That, or you just decided to find a question that's vaguely similar and say it's a possible duplicate... – josh Dec 12 '14 at 09:30
  • Personally, I think it's an exact duplicate, but we'll let the community decide. –  Dec 12 '14 at 10:55

2 Answers2

5

That warning is to make sure that you have not mistyped = instead of == or ===.

Instead, you can get the boolean value of the evaluated result, like this

while (!!(obj = doSomething())) {
thefourtheye
  • 233,700
  • 52
  • 457
  • 497
  • Ah, thanks! I wish I could just use my original syntax without triggering an error but this is a good compromise without using JSHint specific comments. – josh Dec 12 '14 at 06:54
  • Actually, you don't need the `!!`; the extra set of parentheses are enough to shut jshint up. –  Dec 12 '14 at 07:25
-2

The single = assigns value and is not a comparison operator. Use the below:

while (obj == doSomething()) {
  // something with obj
}

Refer: http://www.w3schools.com/js/js_comparisons.asp

Playmaker
  • 1,458
  • 1
  • 14
  • 23
  • 1
    It appears you don't understand the question, the OP knows very well the difference. They are purposely using `=`. – Spencer Wieczorek Dec 12 '14 at 06:49
  • Oh ok.. Wouldn't that result in an infinite loop then? (I am assuming the other case would assign the value and evaluate the boolean as true always) – Playmaker Dec 12 '14 at 06:51
  • 3
    Actually, this answer is a perfect example of why it's a bad idea - it could be misinterpreted as unintentional. – slebetman Dec 12 '14 at 06:53
  • 1
    @Playmaker: Not if a) there is a `break` statement somewhere in the body or b) `doSomething()` returns a different value for each call and probably `null` when there are no more results. – Felix Kling Dec 12 '14 at 06:53
  • @Playmaker The result of `doSomething` could very well change from iteration to iteration and is reevaluated everytime, so this does not need to be an infinte loop. – Dennis Dec 12 '14 at 06:54
  • Hmm.. a (somewhat) explicit break would be required..Thanks! – Playmaker Dec 12 '14 at 06:54
  • The while loop can modify some values used in the doSomething method. Whilst the intention is not obvious, the loop can or cannot end. – Silviu Burcea Dec 12 '14 at 06:54
  • For practical context, I'm using this kind of loop specifically for reading binary buffers where the result of the `doSomething()` function will either return data (and assign to `obj`) or return nothing and end the loop. – josh Dec 12 '14 at 06:56
  • 1
    Oh. That explains it. Maybe even I'll use it sometime in the future :D – Playmaker Dec 12 '14 at 06:58
  • @slebetman I don't agree with you. Doing this is not a bad idea, would you say when people do: `while($row = $mysqli->query($query))` in PHP is a bad idea? No, it's a convenient one. This *method* has it places. – Spencer Wieczorek Dec 12 '14 at 06:58