I would imagine this is a pretty standard Javascript logical operator example, but I found it funny that a typo made the answer correct. When bored = fasle (false spelt incorrectly), the code executed correctly. Why does "fasle" work? Any input is appreciated.
var tired = false; var bored = true; var nap = function() { nap = tired || bored; bored = fasle; //tired = false; if ( nap = true) { console.log("they're both true!"); } else ( nap = false); { console.log("they're false!"); } };
fasle
is evaluated as undefined
, which then evaluates to false when converted to boolean.
But false === fasle
evaluate to false, because they have different types.
This will give you an error in strict mode javascript, because undefined variables are not allowed in this case.
I guess your script can run "correctly" do have something to do with the browser you're using , which I denied at first. This will just cast an error, when tested on safari and chrome.
BTW, if ( nap = true)
will always be true and if ( nap = false)
will always be false. This is because =
is assign operator while ==
is the equality comparison operator you want. Assign operator will always evaluate to the right operand.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.