Js Boolean


What is Boolean?

Boolean is a wrapper class. Its constructor checks conditions and returns true if the condition or expression is true. Boolean controls the flow of the program through condition check. You can initialize a boolean object using the “new” keyword.

Var bool = new Boolean(true);

Boolean treats Null, undefined and (“”) as false values. Boolean stores only one value at a time, either true or false.

Significance of Boolean

Boolean is a wrapper class that checks if the condition is true or not, it might check conditions or expressions or values or objects and show the result. Boolean returns only two values, either true or false.

  • Constructor : Boolean()

Boolean wrapper class contains various methods used for performing various operations on boolean values.

Example
  • var bool = new Boolean(true); //initialise variable to value true.
                alert(bool); //will return true
  • varb1= new Boolean(""); // returns false 
  • var b2= new Boolean(0); //returns false 
  • var b3= new Boolean(undefined); // returns false 
  • var b4= new Boolean(NULL); // returns false 
  • var b5= new Boolean(NaN); // returns false 
  • var b6= new Boolean(“ABC"); // returns true 
  • var b7= new Boolean(1); // returns true

Future scope in javascript

Javascript is a good front end and a backend language; there are a lot of career chances in Javascript and its framework. If you want to learn javascript, there are various platforms available to learn it properly.

Example No.1:

<html> <body> <h2>Knowledge2life Website</h2> <p>Display the value of Boolean(10 > 9):</p> <p id="demo"></p> <script> document.getElementById("demo").innerHTML = Boolean(10 > 9); </script> </body> </html>

OUTPUT:

Knowledge2life Website

Display the value of Boolean(10 > 9):
true

Example No.2:

<html> <body> <h2>Knowledge2life Website</h2> <p>Display the Boolean value of 0:</p> <p id="demo"></p> <script> let x = 0; document.getElementById("demo").innerHTML = Boolean(x); </script> </body> </html>

OUTPUT:

Knowledge2life Website

Display the value of Boolean(10 > 9):
false