Updated April 13, 2023
Introduction to JavaScript Boolean()
JavaScript Boolean() refers to the two boolean values: true or false as the name suggests. For programming in general, this boolean value can be interpreted in various other naming conventions like Yes/No, On/Off, or True/False. But in JavaScript, the Boolean only means true or false. Hence it can be used to assert if the required function is true or not. In JavaScript, it is a primitive data type.
Basically, it can be used to control the program flow by using conditional statements like loops: switch, if..else, do..while and while. We can use this Boolean function to get the values of objects, variables, expressions, and conditions in the form of true or false.
Syntax:
Boolean(variable/expression)
Constructors of JavaScript Boolean()
Below are the Constructors of JavaScript Boolean():
1. Boolean toString() Method
This method is used for returning the particular Boolean object.
Below is the example:
Code:
<html>
<head>
<title>Example for toString() Method</title>
</head>
<body>
<script type = "text/javascript">
var fl = new Boolean(false);
document.write( "fl.toString is : " + fl.toString() );
</script>
</body>
</html>
Output:
Explanation: In this example, we are first defining our script type as text format. Then we are using a flag variable to first assign its value to false. We are then writing the same string value in the output using the function toString().
There is another method specific to the local environment which is:
toLocaleString(): This method returns a string of boolean values in the browser of the local environment.
2. Boolean toSource() Method
This method is used to return a string that indicates the source code of the required object.
A thing to note here that this method is not feasible with all the browsers.
Below are the example:
Code:
<html>
<head>
<title>Example for toSource() Method in JavaScript</title>
</head>
<body>
<script type = "text/javascript">
function novel(name, author, cost) {
this.name = name;
this.author = author;
this.cost = cost;
}
varnewNovel = new novel("Harry Potter","J.K.Rowling",600);
document.write(newNovel.toSource());
</script>
</body>
</html>
Output:
In this example, we are first defining the script type as text format. We define function names with 3 string values namely name, author, and cost. We then create a new object defining its values as required for all 3 strings. We display these string values with the help of toSource function.
3. Boolean valueOf() Function
This method is used to return the primitive value of the given boolean object.
Syntax:
boolean.valueOf()
Return Values: As told, it is used to return the primitive value of the given boolean object.
Below are the examples:
Code:
<html>
<head>
<title>Example for valueOf() Function</title>
</head>
<body>
<script type = "text/javascript">
var fl = new Boolean(false);
document.write( "Boolean fl.valueOf is : " + fl.valueOf() );
</script>
</body>
</html>
Output:
Examples to Implement JavaScript Boolean()
Below are examples of JavaScript Boolean().
Example #1
In this example, we are as usual first defining the format of a script type as text. We are then assigning the default value of flag variable fl as false and then displaying the same using the document write function.
Code:
<!DOCTYPE html>
<html>
<body>
<p id="Example"></p>
<script>
var var1 = Boolean(2);
var var2 = Boolean(2.45);
var var3 = Boolean(-33);
var var4 = Boolean("String");
var var5 = Boolean('true');
var var6 = Boolean(2 + 8 + 0.26);
document.getElementById("Example").innerHTML =
"2 is " + var1 + "<br>" +
"2.45 is " + var2 + "<br>" +
"-33 is " + var3 + "<br>" +
"Whichever string that is non empty is " + var4 + "<br>" +
"The string 'true is' " + var5 + "<br>" +
"Whichever non zero expression becomes " + var6;
</script>
</body>
</html>
Output:
Explanation: In this example, we shall demonstrate how Boolean works for all kinds of values such as integer (2), decimal (2.45), negative number (-33), any non-empty string value (string), boolean value (true), an operation which is having non zero expression (2 + 8 + 0.26). We are passing these above-mentioned values by assigning them to different variables var1 to var6 and then displaying the same using getElementBy function. Hence in the output, we can see that all the boolean values are being returned true because all of the variables have values and none of them are null.
Example #2
Code:
<!DOCTYPE html>
<html>
<body>
<p>Here we display the Boolean value of 0:</p>
<button onclick="myFunction()">Click here</button>
<p id="example"></p>
<script>
function myFunction() {
var var1 = 0;
document.getElementById("example").innerHTML = Boolean(var1);
}
</script>
</body>
</html>
Output:
After clicking on the button
In this example, we are displaying what happens when we pass a Boolean value of 0 to the variable. As seen in the output it returns the value false.
Example #3
Code:
<!DOCTYPE html>
<html>
<body>
<p>Check when to display value of Boolean -0:</p>
<button onclick="myFunction()">Try it</button>
<p id="example"></p>
<script>
function myFunction() {
var var1 = -0;
document.getElementById("example").innerHTML = Boolean(var1);
}
</script>
</body>
</html>
Output:
After clicking on the button
In this example, we are demonstrating what happens when we try to display the value of boolean -0. It returns false.
Example #4
Code:
<!DOCTYPE html>
<html>
<body>
<p>Check the value of Boolean NaN value:</p>
<button onclick="myFunction()">Try it</button>
<p id="example"></p>
<script>
function myFunction() {
var var1 = 20 / "H";
document.getElementById("example").innerHTML = Boolean(var1);
}
</script>
</body>
</html>
Output:
After clicking on the button
In this example, we shall see what value is returned when we pass a value of NaN (Not a Number) in PHP. It returns the value false as seen in the output.
Example #5
Code:
<!DOCTYPE html>
<html>
<body>
<p>Check the value of undefined Boolean variable:</p>
<button onclick="myFunction()">Click here</button>
<p id="example"></p>
<script>
function myFunction() {
var var1;
document.getElementById("example").innerHTML = Boolean(var1);
}
</script>
</body>
</html>
Output:
After clicking on the button
In this example, we can see what happens when we declare a variable called var1 and not assign it anything. When we display this variable using the Boolean method it returns false as seen in the output.
Conclusion
As seen in the above examples, Boolean in JavaScript is used for converting a parameter into a boolean value i.e, true or false. Hence any variable or object which has a value assigned to it will always return true and the one which is having no value or others as defined above will return false. We have also discussed the various constructors along with their uses.
Recommended Articles
This is a guide to JavaScript Boolean(). Here we discuss the Introduction to JavaScript Boolean() and how JavaScript Boolean() works along with Examples and Code Implementation. You can also go through our other suggested articles to learn more –