Error: Expected a conditional Expression and Instead saw an assignment

Asked

Viewed 190 times

1

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

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

error Expected a conditional expression and instead saw an assignment.

  • This is a jslint error, no?

  • My code is in a single line, so I played in jsbin and gave it

1 answer

3


It is not possible to evaluate if your code is correct or not only by this excerpt, but what you are seeing is an error presented by a tool linting, probably the Jslint or the Jshint. It is not a syntax error. Your code may or may not be working properly, but these tools have no way of knowing. So 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 allocation operator (=) within an expression that expects a boolean value (in this case the while, but it could be a if or a for). You may actually want to assign a value to match, but it is also possible that you actually wanted 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

if(x == 5)

or

if(x === 5)

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

More details on Jslint Errors.


Note: you commented that you used Jsbin in the test; its error panel uses a linter, so you saw this error there.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.