Menu Close

JavaScript Boolean

In this tutorial, you will learn about JavaScript Boolean. JavaScript boolean means one of two values True or False. In the previous tutorial, we have seen lots of JavaScript topics but here we will learn all about JavaScript boolean.

JavaScript Boolean

JavaScript Boolean represent one of two values: True or False.

The Boolean() Function

Boolean() is used to find out if an expression is true.

Example

<!doctype html> 
<html> 
  <head>
  <title> JavaScript Boolean</title>
  </head> 
  <body> 
<p id="myID"></p>
  </body> 
<script>
	document.getElementById('myID').innerHTML = Boolean(10 > 4);
</script>
</html>

Everything Without A Value is False

The Boolean value of 0 is False.

<!DOCTYPE html>
<html>
<body>

<p>Display the Boolean value of -0:</p>
<button onclick="myFunction()">Try it</button>
<p id="demo"></p>
<script>
function myFunction() {
  var x = -0;
  document.getElementById("demo").innerHTML = Boolean(x);
}
</script>

</body>
</html>

The Boolean value of empty string (” “) is false.

<!doctype html> 
<html> 
  <head>
  </head> 
  <body> 
<p id="myID"></p>
  </body> 
   <script>

document.getElementById('myID').innerHTML = Boolean("");

</script>
</html>

The Boolean value of undefined is false.

<!doctype html> 
<html> 
  <head>
  </head> 
  <body> 
<p id="myID"></p>
  </body> 
   <script>
var x;
document.getElementById('myID').innerHTML = Boolean(x);

</script>
</html>

The Boolean of null is false.

<!doctype html> 
<html> 
  <head>
  </head> 
  <body> 
<p id="myID"></p>
  </body> 
   <script>
var x = null;
document.getElementById('myID').innerHTML = Boolean(x);

</script>
</html>

The Boolean value of false is false.

<!doctype html> 
<html> 
  <head>
  </head> 
  <body> 
<p id="myID"></p>
  </body> 
   <script>
var x = false;
document.getElementById('myID').innerHTML = Boolean(x);

</script>
</html>

The Boolean value of NaN is false.

<!doctype html> 
<html> 
  <head>
  </head> 
  <body> 
<p id="myID"></p>
  </body> 
   <script>
var x = 10/ "H";
document.getElementById('myID').innerHTML = Boolean(x);

</script>
</html>

Booleans Can be Objects

You can defines Boolean as object with new keyword.

<!doctype html> 
<html> 
  <head>
  </head> 
  <body> 
<p id="myID"></p>
  </body> 
   <script>
var x = false;
var y = new Boolean(false);
document.getElementById('myID').innerHTML = typeof x + "<br>" + typeof y;

</script>
</html>

Conclusion

Here we have seen all about JavaScript Boolean along with various examples.Boolean has represent the only two values true or false.

JavaScript Boolean play a most important role in JavaScript program, for example, logic building, condition, etc.
If you learning JavaScript, Then you can’t ignore boolean. If you like this article, please share and keep visiting for further JavaScript interesting tutorials.

Reference:- Click Here

Python File Delete
Python Arithmetic Operators

Related Posts