0
Javascript Boolean
I want to know why this is in if condition var a = new Boolean(false); if (a) { alert('hello'); } else { alert('world'); } I think here we are creating a new object which does not depend on argument value so it always returns true unless we assign the value of a to false am I right? or any other explanation about that?
1 Resposta
+ 5
Any object of which the value is not undefined or null, including a Boolean object whose value is false, evaluates to true when passed to a conditional statement. For example, the condition in the following if statement evaluates to true:
var x = new Boolean(false);
if (x) {
// this code is executed
}
You can read more on:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean