var m = 5;
if (m) {
document.write(m);
}
In my view, m is not a bool type, therefore, this if statement will not be executed. However, I do see the value of m printed on my screen. Why?
var m = 5;
if (m) {
document.write(m);
}
In my view, m is not a bool type, therefore, this if statement will not be executed. However, I do see the value of m printed on my screen. Why?
You should read about truthy values (MDN).
In JavaScript, a truthy value is a value that is considered
true
when encountered in a Boolean context. All values are truthy unless they are defined as falsy (i.e., except forfalse
,0
,0n
,""
,null
,undefined
, andNaN
).
Any number other than 0 is truthy, so if(5)
is the same as if(true)
.