JavaScript: true/false (boolean)

By Xah Lee. Date: . Last updated: .

true and false are builtin Primitive Values .

Type of true and false is "boolean", and they are the only possible values of boolean Type .

console.log( (typeof true) === "boolean" ); // true
console.log( (typeof false) === "boolean" ); // true

Values in Boolean Context

When a value is evaluated in boolean context, such as in a if statement, the function Boolean is called on the value, and returns true or false

 if ( 3 ) {console.log( "y" ); } else {console.log( "n" );} // y 

False are:

// all of the following prints false.

console.log( Boolean(0) );
console.log( Boolean(0.0) );
console.log( Boolean(0.) );
console.log( Boolean(.0) );

console.log( Boolean("") );

console.log( Boolean(null) );
console.log( Boolean(undefined) );
console.log( Boolean(NaN) );

Everything else is true.

// all of the following prints true

console.log( Boolean(1) );
console.log( Boolean(-1) );
console.log( Boolean(Infinity) );
console.log( Boolean({}) ); // empty object
console.log( Boolean([]) ); // empty array
console.log( Boolean(function () {}) ); // function object

JavaScript Boolean

JavaScript Operators

BUY
Ξ£JS
JavaScript in Depth

JavaScript in Depth

Basic Syntax

Value Types

Variable

String

Property

Object and Inheritance

Array

Function

Constructor/Class

Iterable 🌟

Misc