This javascript console log is confusing me. How can x which has the value of -1 be evaluated to true in the if statement where it false the line before it? Thanks
x = -1
-1
x == true
false
if(x){console.log('yes')}
yes
This javascript console log is confusing me. How can x which has the value of -1 be evaluated to true in the if statement where it false the line before it? Thanks
x = -1
-1
x == true
false
if(x){console.log('yes')}
yes
When you test x
in your if statement, you're not checking if x
is true, you're checking if x
is truthey. The rules for truthiness in javascript are:
empty strings are falsey. all other strings are truthy.
0
and NaN
are falsey. all other numbers are truthy.
all objects and arrays are truthy.
null
and undefined
are falsey.