9.2 ToBoolean

2010-05-04

true|false ToBoolean(input:mixed)

Convert the input to a boolean. Always returns true or false.

Code: (Meta Ecma)
function ToBoolean(input:mixed){
// a few inputs will return false, anything else returns true (including any and all objects!)
if (input === undefined || input === null || input === false || input === +0 || input === -0 || isNaN(input) || input === '') return false;
return true;
}

Most important thing to note here is that any input of type Object will return true. Also note that +0 is equal to 0 (so don't be fooled into thinking the specification skipped zero being false).

Even though an object should always evaluate to boolean true, there are certain cases in which the object is forced into another object before it is evaluated. In such cases the expected behavior might deviate from what's explicitly defined here.