Error: Expected a conditional expression and instead saw an assignment

1

I am "debugging" my jQuery codes and I came across this error in the following line

while(match = regex.exec(item)) {
  [...]

error Expected a conditional expression and instead saw an assignment.

    
asked by anonymous 28.11.2014 / 22:06

1 answer

3

You can not evaluate whether your code is correct or not just for this snippet, but what you're seeing is an error presented by a linting tool, probably JSLint or the JSHint . This is not a syntax error. Your code may be working correctly or not, but these tools have no way of knowing. This is why they point you to the location of a possible problem, and advise you not to use this type of syntax.

The problem itself is the assignment operator ( = ) within an expression that expects a Boolean value (in this case while , but could be a if or a for ). It is possible that you actually want to assign a value to match , but you may also actually want to compare the value of match with what is on the other side of the operator. A clearer example, with if , is when someone writes:

if(x = 5)

When you really mean it

if(x == 5)

or

if(x === 5)

The first case would be a logic error (the code will always enter if ), and linter wants to prevent you from committing this type of error by prohibiting assignment within if or initiators for and while .

More details on JSLint Errors .

Note: You commented that you used JSbin in the test; his bug panel uses a linter, so you saw that bug there.

    
28.11.2014 / 22:18