/

HTML / CSS / JavaScript Tutorial

JavaScript Tutorial: Booleans

[this page | pdf | back links]

JavaScript Boolean variables take one of two values, true or false. They are instantiated by a statement such as:

 

var b = true;

 

You can usually use the global Boolean() function to identify whether an expression is true or false, although it is simpler just to use operators that return Boolean outputs, e.g. Boolean(2 > 1), (2 > 1) or even 2 > 1 all return true. It is worth noting that the global Boolean() function returns an object and this can in some circumstances behave counterintuitively relative to the primitive Boolean values of true and false.

 

Boolean objects support the following properties and methods:

 

Properties:

 

Property

Description

More

constructor

Returns object’s constructor function

Here

length

Returns the length of a Boolean object

Here

prototype

Allows author to add properties and methods to an object

Here

 

Methods:

 

Method

Description

More

toString()

Converts boolean value to a string

Here

valueOf()

Returns the primitive value of the object

Here

 


NAVIGATION LINKS
Contents | Prev | Next | JavaScript Booleans


Desktop view | Switch to Mobile