Curriculum
Course: JavaScript Basic
Login

Curriculum

JavaScript Basic

JSHome

0/216
Text lesson

JavaScript Booleans as Objects

The Boolean value of false is, as expected, false.

let x = false;

However, booleans can also be created as objects using the new keyword.

let y = new Boolean(false);

Example

let x = false;
let y = new Boolean(false);

// typeof x returns boolean
// typeof y returns object

Avoid creating Boolean objects.

Using the new keyword complicates the code and can slow down execution.

Boolean objects may lead to unexpected results.

When using the == operator, x and y are considered equal.

let x = false;
let y = new Boolean(false);

When using the === operator, x and y are not considered equal.

Pay attention to the difference between (x == y) and (x === y).

Is (x == y) true or false?

let x = new Boolean(false);
let y = new Boolean(false);

Is (x === y) true or false?

let x = new Boolean(false);
let y = new Boolean(false);