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.
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.
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 javascript
You are not signed in. Login or sign up in order to post.
This is a jslint error, no?
– bfavaretto
My code is in a single line, so I played in jsbin and gave it
– Vinícius Lara