I noticed this behavior in Javascript today which has me thoroughly surprised and confused:
if ("0") {
console.log('first')
}
if ("0" == true) {
console.log('second');
}
output:
first
I would have expected either both or neither of the console.log
lines to execute. I had always believed that the contents of a guard is evaluated, coerced into a boolean, and then passes or fails based on the value of this boolean. This would result in both of the guards above being equivalent, but the observed behavior indicates something else is going on.
How does Javascript determine whether a guard passes or fails?