JavaScript – Boolean


What is Boolean ?

Its a binary value can have values 0 and 1, 0 represents false and 1 represents true.

What is Boolean in Javascript ?

Boolean is a Javascript built-in object that represents true/false values.

Boolean can hold only two values true or false.

The default value of boolean object is false.

Boolean object can be created by using Boolean constructor.

Why is Boolean object used in javascript ?

Boolean value controls the program flow for various conditions.

How to construct a Boolean Object in javascript ?

Boolean Object can be created by using

1. By Boolean() constructor.

Learn javascript free online !!!

Click to Learn More about – Javascript online learning

1. By Boolean() constructor.

We can create boolean object by using boolean constructor.

Syntax :


Boolean {variable_name}  = new Boolean({value});  

Example :

<!DOCTYPE html>
<html>
	<head>
		<title>Our title</title>
		<script type="text/javascript">
			var a = 10; // Numeric value
			var b = 37; // Numeric value
			document.write("The value of a = "+a+"<br/>");
			document.write("The value of b = "+b+"<br/>");
			document.write("The Boolean value of b greater than a  = "+(a<b)+"<br/>");
			document.write("The Boolean value of a greater than b  = "+(a>b)+"<br/>");
		</script>
	</head>
	<body></body>
</html>

Output :

The value of a = 10

The value of b = 37

The Boolean value of b greater than a = true

The Boolean value of a greater than b = false

What are the default values for Boolean ?

By default Boolean value is “false”.

The Boolean value of 0 (zero) is false.

The Boolean value of -0 (minus zero) is false.

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

The Boolean value of undefined is false.

The Boolean value of null is false.

The Boolean value of false is (you guessed it) false.

The Boolean value of NaN is false.

Example :


<!DOCTYPE html>
<html>
	<head>
		<title>Our title</title>
		<script type="text/javascript">
			var a = 0;
			document.write("The Boolean value of a (0) = "+Boolean(a)+"<br/>");
			var b = -0;
			document.write("The Boolean value of b (-0) = "+Boolean(b)+"<br/>");
			var c = "";
			document.write("The Boolean value of c (empty string) = "+Boolean(c)+"<br/>");
			var d;
			document.write("The Boolean value of d (undefined) = "+Boolean(d)+"<br/>");
			var e = null;
			document.write("The Boolean value of e (null) = "+Boolean(e)+"<br/>");
			var f = false;
			document.write("The Boolean value of f (false) = "+Boolean(f)+"<br/>");
			var g = 10/"A";
			document.write("The Boolean value of g (Not a Number NaN) = "+Boolean(g)+"<br/>");
		</script>
	</head>
	<body></body>
</html>

Output :

The Boolean value of a (0) = false

The Boolean value of b (-0) = false

The Boolean value of c (empty string) = false

The Boolean value of d (undefined) = false

The Boolean value of e (null) = false

The Boolean value of f (false) = false

The Boolean value of g (Not a Number NaN) = false

There are few boolean methods in javascript listed below,

SnoMethod/ FunctionDescription
1toSource()toSource() method returns the source of Boolean object as a string.
2toString()toString() method converts Boolean into String.
3valueOf()valueOf() method converts other type into Boolean.