Join MDN and developers like you at Mozilla's View Source conference, 12-14 September in Berlin, Germany. Learn more at https://viewsourceconf.org

Boolean

The following code actually produces a "false" response:

function booleanObject(){ x = new Boolean(false); if (x) alert(x);

  }

This seems to contradict the following text from the page at Mozilla.org on booleans. Can anyone clarify whether boolean objects actually equate as true when a conditional statement is used if the object is stated as false?

<hr>

Description

Do not confuse the primitive Boolean values true and false with the true and false values of the Boolean object.

Any object whose 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:

x = new Boolean(false); if (x) //the condition is true

This behavior does not apply to Boolean primitives. For example, the condition in the following if statement evaluates to false:

x = false; if (x) //the condition is false

<hr>

The point is:

function checkFalse(x) {
  if(x)
    alert("hey!");
}
checkFalse(new Boolean(false)); // this shows an alert
checkFalse(false);              // this does not

--Nickolay 08:41, 19 November 2005 (PST)

Document Tags and Contributors

 Contributors to this page: Nickolay, Meandmymachine
 Last updated by: Nickolay,