A Platform To Learn To Code

JAVASCRIPT BOOLEAN



The boolean in javascript is a primitive data type that has two values true representing yes and false representing no.

boolean is generally used in conditional statements or at place when we have to control the flow example loops, if-else, switch etc.

Example

Run the code

The Boolean Constructor

Boolean() is a function which can be used to create boolean values.

The Boolean() function can receive variables or expressions as arguments.

Example: Boolean constructor

Run the code

A variable or any object which has some value is considered as true in javascript and one which has no value is considered as false example 0, undefined, null, "" etc.

Lets see an example.

Example

Run the code

Boolean as Object

Generally Boolean is a primitive data type when created using literal. Example var bool = true;.

But booleans can be made objects using new keywords. Example var boolObj = new Boolean(true);.

Example

Run the code

Use literals to define boolean because boolean as object is slow

Boolean objects are not equal to boolean literals and also boolean objects are slower.

Example

Run the code