aizquier
aizquier

Reputation: 567

if executes despite the condition is false

Edit: mistery solved! I had put a semicolon after the if(). Thanks Paul!

I just have arrived to this curious situation (the snippet can be run at http://jsfiddle.net/nUs7h/ ):

function check_if_tag_used()
{
   var found = false;   
   return found;   
}

var used = check_if_tag_used();
console.debug(used);

if (used);
{
   alert("This should not appear!"); // why this runs?   
}

Why the alert() is displayed despite that the value of the variable used if false? Notice that console.debug() reports it as false indeed.

Upvotes: 1

Views: 99

Answers (1)

Paul Draper
Paul Draper

Reputation: 83225

You have a semicolon at the end of your if.

if (used);

Remove it.

FYI, this is why I always use the same-line brace style

if (used) {
   alert("This should not appear!");
}

It's much harder for this to happen.

Upvotes: 8

Related Questions