Advertisement

Advertisement


JavaScript Booleans

A Javascript boolean represents only two values: True or False. It can be created using the Boolean() function.
Booleans can be created using literals or their object can also be created. Given below is the syntax of creating boolean objects.

Syntax
var val = new Boolean(value);

The Boolean() Function

The Boolean() function can be used to find out whether an expression (or a variable) is true. In the example below, the booleans are created using literals, have a look at it:


<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title> JavaScript Boolean </title>
</head>
<body>
<p>Display the value of 20 > 10:</p>
<button onclick="booleanFun()">Try it</button>
<p id="demo"></p>
<script>
function booleanFun() {
document.getElementById("demo").innerHTML = 10 > 9;
}
</script>
</body>
</html>

Output

JavaScript Boolean

Display the value of 20 > 10:


Boolean Properties

Property Description
Constructor It Returns the reference of Boolean function that created Boolean object.
Prototype It enables you to add properties and methods in Boolean prototype.

Boolean Methods

Property Description
toSource() It returns the source of Boolean object as a string.
toString() It converts Boolean into String.
valueOf() It returns the primitive value of a Boolean.

Booleans Values

TRUE: If there is a value present in a literal or variable, then the boolean result will be true. Ex. values like 1, 15, "Code", 1.2, -6, etc., all the values will produce output as TRUE.

FALSE: If there is no value present or even if the value is 0, then the output will be false. Ex. values like 0, -0, null, undefined, false, NaN(Not a number), empty string, etc., all the values will produce output as FALSE.


Boolean Objects

As stated earlier, booleans object can also be created. The new keyword is used to create a boolean object.

Note : Booleans objects however are not recommended as they slow down the loading of webpage.







Become a Patron!

Note: If the updated content is not visible to you please refresh the cookies by pressing CTRL + Shift + R.