A Javascript boolean represents only two values: True
or False
. It can be created using the Boolean()
function. Booleans can be created using literals or their object can also be created. Given below is the syntax of creating boolean objects.
The Boolean()
function can be used to find out whether an expression (or a variable) is true. In the example below, the booleans are created using literals, have a look at it:
Display the value of 20 > 10:
Property | Description |
---|---|
Constructor | It Returns the reference of Boolean function that created Boolean object. |
Prototype | It enables you to add properties and methods in Boolean prototype. |
Property | Description |
---|---|
toSource() | It returns the source of Boolean object as a string. |
toString() | It converts Boolean into String. |
valueOf() | It returns the primitive value of a Boolean. |
TRUE: If there is a value present in a literal or variable, then the boolean result will be true. Ex. values like 1, 15, "Code", 1.2, -6, etc., all the values will produce output as TRUE
.
FALSE: If there is no value present or even if the value is 0, then the output will be false. Ex. values like 0, -0, null, undefined, false, NaN(Not a number), empty string, etc., all the values will produce output as FALSE
.
As stated earlier, booleans object can also be created. The new
keyword is used to create a boolean object.
Follow Us: