JavaScript Tutorial
JS - Introduction JS - Basic JS - Placement JS - Output JS - Statements JS - Syntax JS - Variables JS - Comments JS - Data Types JS - Operators JS - Operator Precedence JS - Condition JS - Switch JS - Functions JS - Objects JS - Loops For JS - Loops While JS - Break JS - Strings JS - String Methods JS - Numbers JS - Number Methods JS - Arrays JS - Array Methods JS - Array Iteration JS - Events JS - Event Listener JS - Event Propagation JS - Date JS - Math JS - Random JS - Boolean JS - Type Conversion JS - RegExp JS - Exception JS - Scope JS - Debugging JS - Hoisting JS - Strict Mode JS - this Keyword JS - Mistakes JS - Best Practices JS - Performance JS - Form Validation JS - ES6 Features
JS Objects
Object Definitions Object Properties Object Methods Object Constructors Object Prototypes
JS Functions
Function Definitions Function Parameters Function Call Function Apply Function Closures
DOM Introduction DOM Methods DOM Selectors DOM HTML DOM CSS DOM Attributes DOM Navigation
JS Browser BOM
JS - Window JS - Screen JS - Location JS - History JS - Navigator JS - Popup Alert JS - Timing JS - Cookies
AJAX - Introduction AJAX - XMLHttp AJAX - Request AJAX - Response AJAX - PHP
JSON - Introduction JSON - Syntax JSON - Data Types JSON - Parse JSON - Stringify JSON - Objects JSON - Arrays JSON - PHP JSON - JSONP
JS References
JavaScript Reference JavaScript Methods HTML DOM Reference

JavaScript Booleans

In computer science, a Boolean is a logical data type that can have only the values true or false.

For this, JavaScript has a Boolean data type. It can only take the values true or false.

Run code

The Boolean() Function

The Boolean() function can be used to find out if an expression is true:

Run code

OR you can also use the following syntax:

Run code

Comparisons and Conditions

In JavaScript, Boolean conditionals are often used to decide which sections of code to execute (such as in if statements) or repeat (such as in for loops).

Below is some JavaScript pseudo code (it's not truly executable code) demonstrating this concept.

Below are some examples of boolean conditional:

Operator Description Example
== equal to if (month == "July")
> greater than if (age > 18)
< less than if (age < 18)

Everything With a "Value" is True

All values, including any object or the string "false", create an initial value of true.

Run code

Everything Without a "Value" is False

If a value is omitted or is 0, -0, null, false, NaN, undefined, or the empty string (""), the object has an initial value of false.

The Boolean value of 0 (zero) is false:

Run code

The Boolean value of -0 (minus zero) is false:

Run code

The Boolean value of null is false:

Run code

The Boolean value of false is false:

Run code

The Boolean value of NaN is false:

Run code

The Boolean value of undefined is false:

Run code

The Boolean value of empty string ("") is false:

Run code

Boolean Primitives and Boolean Objects

Normally, JavaScript booleans are primitive values, created from literals:

var x = false;

But booleans can also be defined as objects using the new keyword:

var x = new Boolean(false);

In order to test the difference between the two, we will initialize a boolean primitive and a boolean object.

Run code

Note: Don't create booleans as objects. It slows down execution speed and can produce some unexpected results.

When using the == operator, equal booleans are equal:

Run code

When using the === operator, equal booleans are not equal, because the === operator expects equality in both value and type:

Run code

Objects cannot be compared:

Run code

Notice the difference between (==) and (===). Comparing two JavaScript objects will always return false.

Complete Boolean Reference

For a complete properties and methods reference, visit our JavaScript Boolean Reference.

The reference section contains descriptions and examples of all Boolean properties and methods.