JavaScript Booleans

The JavaScript Boolean represents two values : true and false. We can create JavaScript Boolean just like normal variable or with boolean constructor().
  var x = true;
Creating Boolean variable with constructor :
  var x = new Boolean(true);
  var y = new Boolean(false);

Boolean Function :

The Boolean() function is used to find out if an expression or a variable is true or false. For example The below statement
  Boolean(100 <= 101)
The above statement will return true. Lets see some example :
<script>
  document.write("Boolean(100 >= 101) : ", Boolean(100 <= 101));		
  document.write("<br/>Boolean(200 == 201) : ", Boolean(200 == 201));
  document.write("<br/>Boolean(180 != 180) : ", Boolean(180 != 180));
  document.write("<br/>Boolean(20 == 20) : ", Boolean(200 == 200));
</script>
Output :


Also note that if a variable is undefined or contain '0' value or null value than it will return false. For example :
  var x = "";
  Boolean(x);
The above statement will return false because the variable x contain nothing. Similarly
<script>
  var x = '';
  document.write("Boolean(x) : ", Boolean(x));
  var y;
  document.write("<br/>Boolean(y) : ", Boolean(y));
  var z = 0;
  document.write("<br/>Boolean(z) : ", Boolean(z));
</script>
Output :


And if the variable will contains any value, than it will return true.
<script>
  var x = -12.32;
  document.write("Boolean(x) : ", Boolean(x));
  var y = "Hello world";
  document.write("<br/>Boolean(y) : ", Boolean(y));
  var z = 12.322;
  document.write("<br/>Boolean(z) : ", Boolean(z));
</script>
Output :



Next Topic :