JavaScript Boolean


You will need a data type that can only have one of two values, like-
  • YES / NO
  • ON / OFF
  • TRUE / FALSE
The basic theory
In Boolean logic, a statement can have two values, true or false. When using Boolean logic in philosophy, the statement can be a sentence,Like:-
It rains today.
Ans will be – yes / no (the condition may be true or false )

JavaScript Boolean is an object that represents value in two states: true or false. You can create the JavaScript Boolean object by Boolean() constructor as given below.Boolean b=new Boolean(value);  
The default value of JavaScript Boolean object is false.

<!DOCTYPE html>
<html>
<body>

<p>Display the Boolean value as result of following condition(100 > 900):</p>

<button onclick="myFunction()">click it</button>

<p id="output"></p>

<script>
function myFunction() {
    document.getElementById("output").innerHTML = Boolean(100 > 900);
}
</script>

</body>
</html>

JavaScript Boolean Example

<script> 
document.write(10<20);//true 
document.write(10<5);//false 
</script> 

JavaScript Boolean Properties

Property
Description
constructor
constructor           returns the reference of Boolean function that created Boolean object.
prototype
                enables you to add properties and methods in Boolean prototype.

JavaScript Boolean Methods

Method
Description
toSource()
returns the source of Boolean object as a string.
toString()
converts Boolean into String.
valueOf()
converts other type into Boolean.