Booleans

The boolean primitive is a logical data type with only two values: true and false.

All values in JavaScript are implicitly true or false. The Boolean object can be used to coerce a value to a true or false boolean, based on the implicit true or false state of that value:

Boolean( "A string literal" );
> true

Values that result in false include 0, null, undefined, NaN, an empty string (""), an omitted value, and a false boolean. All other values result in true.

Boolean( NaN );
> false

Boolean( -0 );
> false

Boolean( 5 );
> true

Boolean( "false" ); // the value `"false"` is a string, and therefore implicitly true.
> true

Avoid using the Boolean object as a constructor. It creates an object containing a boolean value, not the boolean primitive you might expect:

const falseBoolean = Boolean( 0 );
const falseObject = new Boolean( 0 );

console.log( falseBoolean  );
> false

console.log( falseObject  );
> Boolean { false }

falseObject.valueOf();
> false

Because all objects are inherently truthy, the resulting boolean object always loosely evaluates to true, even if it contains a false value:

const falseBoolean = Boolean( 0 );
const falseObject = new Boolean( 0 );

console.log( falseBoolean == true );
> false

console.log( falseObject == true );
> true

Check your understanding

Which of the following returns false?

0
Null.
An empty string
"none".