Tutorials
JavaScriptIn this JavaScript tutorial you will learn about JavaScript boolean object, how to pass value to the boolean object, properties and methods used with boolean object.
The Boolean object is a wrapper for the Boolean data type. The Boolean object is used to convert a non-Boolean value to a Boolean value. If a programmer wants to convert a non-Boolean value to a Boolean value, he can use Boolean as a function to perform this task, instead of using a Boolean object to convert a non-Boolean value to a Boolean value.
The Boolean object returns false when the passed value to the object is any one of the following:
Other than the above values, any other value passed to the Boolean Object returns true. The programmer must clearly understand that a Boolean object is different from Boolean data type.
When an object’s value is not undefined or null (including a Boolean object whose value is false), it evaluates to true when passed to a conditional statement.
For example, the condition in the following if statement evaluates to true:
|
The above concept does not apply for Boolean primitives.
For example, the condition in the following if statement evaluates to false:
|
General Syntax for defining the Boolean Object is as follows:
|
Here the keyword new is used to define the Boolean object.
For example:
|
The above example defines a Boolean Object exforsys.
The general syntax for writing this is as follows:
|
The above statement creates a Boolean Object given in a variable name with the initial value set to the value passed in arguments of the Boolean. The value can be any one of the following:
0, "", false, undefined, NaN, true, any string and so on.
For example:
|
The above example creates two Boolean objects exforsys and sample with initial value as true.
|
The above example creates two Boolean objects exforsys1 and sample1 with initial value as false.