Why the following if statement evaluates to true since the value is false?
var x = new Boolean(false);
if (x) {
// this code is executed
}
from: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean
Why the following if statement evaluates to true since the value is false?
var x = new Boolean(false);
if (x) {
// this code is executed
}
from: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean
It does'nt evaluate to false, since it is an object. the Value of x is an object resulting out of the constructor function:
Boolean {[[PrimitiveValue]]: false}
Objects are allways truthy. instead try grabbing the actual value from your constructed object:
if(x.valueOf()){}
hope this helped
Because x
has become an object, which is a truthy value (i.e not false
).
try:
var x = false;
if (x) {
// this code will not be executed
} else {
// this code will be executed instead
}
Be very careful with your types ;)