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 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>
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>
Boolean data type is mostly used with comparison operators and conditional statements which is described in detail in upcoming chapters.
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>
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>
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>
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>
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>
Note: Boolean values do not have any properties or methods