JavaScript Boolean

The Boolean datatype represents either TRUE or FALSE value.

The Boolean datatype is mostly used with conditional statements to do different actions and change control flow based on condition which returns Boolean TRUE or FALSE

In programming, often we require a variable which holds any one value from two values like..

True / False
Active / Inactive
On / Off
Yes / No 

JavaScript Boolean data type is used to store either true of false value.

Boolean() function

Boolean() function is used to find out if an expression or a variable is true or false.

<!DOCTYPE html>
<html>
<body>

Boolean returns True / False based on expression or variable.
<p id="text"></p>

<script>
var a = 5;
var b = 0;
document.getElementById('text').innerHTML = Boolean(a > b);

</script>

</body>
</html>

Download the code Run the code

An expression always return true or false, it is not necessary to use Boolean() function

<!DOCTYPE html>
<html>
<body>

It is not necessary to use Boolean() function an expression always returns True / False based on expression or variable.
<p id="text"></p>

<script>
var a = 5;
var b = 0;
document.getElementById('text').innerHTML = a > b;

</script>

</body>
</html>

Download the code Run the code

Boolean data type is mostly used with comparison operators and conditional statements which is described in detail in upcoming chapters.

Everything with a real value is True.

All variables having a values other than 0 and empty string is evaluates to true.

<!DOCTYPE html>
<html>
<body>

Real values evaluates to true.
<p id="text"></p>

<script>
document.getElementById('text').innerHTML = '500 is ' + Boolean(500) +
'<br> 2.5 is ' + Boolean(2.5) +
'<br> -50 is ' + Boolean(-50) +
'<br> India is ' + Boolean("India") +
'<br> False is ' + Boolean("India") +
'<br> 1+2+3 is ' + Boolean(1+2+3) +
'<br> 1.5+2.5 is ' + Boolean(1.5+2.5) +
'<br> 1 > 0 is ' + Boolean(1 > 0) +
'<br> -5 < 5 is ' + Boolean(-5 < 5);

</script>

</body>
</html>

Download the code Run the code

Everything without a real value is False.

All variables or expressions having a 0 value or empty string evaluates to false.

<!DOCTYPE html>
<html>
<body>
0 and empty string evalutes to false.
<p id="text"></p>

<script>
document.getElementById('text').innerHTML = '0 is ' + Boolean(0) +
'<br> -0 is ' + Boolean(-0) +
'<br> empty string is ' + Boolean("");

</script>

</body>
</html>

Download the code Run the code

Undefined evaluates to false.

Undefined is always evaluates to false.

<!DOCTYPE html>
<html>
<body>

Undefined evaluates to false.
<p id="text"></p>

<script>
var num;
document.getElementById('text').innerHTML = 'undefined is ' + Boolean(undefined) +
'<br />num is ' + Boolean(num);

</script>

</body>
</html>

Download the code Run the code

Null evaluates to false.

null is always evaluates to false.

<!DOCTYPE html>
<html>
<body>

Null evaluates to false.
<p id="text"></p>

<script>
var start=null;
document.getElementById('text').innerHTML = 'start is null and evaluates to ' + Boolean(start);

</script>

</body>
</html>

Download the code Run the code

NaN evaluates to false.

NaN is always evaluates to false.

<!DOCTYPE html>
<html>
<body>

Null evaluates to false.
<p id="text"></p>

<script>
var sum= 5 * "five";
alert(sum);
document.getElementById('text').innerHTML = 'sum is NaN and evaluates to ' + Boolean(sum);

</script>

</body>
</html>

Download the code Run the code

Note: Boolean values do not have any properties or methods

<<< JavaScript Array MethodsJavaScript Comparisons >>>
Copyright 2005-2016 KnowledgeHills. Privacy Policy. Contact .