In JavaScript, logical information is represented via a data type known as “Boolean.” They come in handy when crafting conditional statements or making logical conclusions in programming. They save one of two values: True or False.
The variables listed below are boolean.
Examples
In this case, x1 and x2 reserve the boolean values – YES and NO. This can also be “True” and “False” or “On” and “Off.” The most common application is “True” and “False.”
var x1 = yes;
var x1 = yes;
Please keep in mind that the variables listed below are set with strings rather than boolean values.
var x1 = “yes”;
var x2 = “no”;
Javascript Boolean Function
JavaScript’s boolean object is just a boolean value wrapped in an object. JavaScript booleans are utilized for data types which have one of two values, such as yes and no values, on and off, true and false, and so on.
The first parameter value will be changed to boolean values. If the value is false, null, zero, false, negative zero, Not-a-Number, or if the value is absent, the object will be assigned a value of false. Any other value will result in the creation of an object with the original value of true. Nevertheless, the true and false values of a Boolean object should not be confused with the basic values of true or false.
Syntax
Boolean(expression/variable)
Illustration 1
The following program will return true values as its output.
<!DOCTYPE html> <html> <body> <script> document.write('Boolean(10) is ' + Boolean(10)); document.write('<br>'); document.write('Boolean("HubToLearn") is ' + Boolean("HubToLearn")); document.write('<br>'); document.write('Boolean(2.74) is ' + Boolean(2.74)); document.write('<br>'); document.write('Boolean(-1) is ' + Boolean(-1)); document.write('<br>'); document.write("Boolean('true') is " + Boolean('true')); document.write('<br>'); document.write("Boolean('false') is " + Boolean('false')); document.write('<br>'); document.write('Boolean(3 * 2 + 1.11) is ' + Boolean(3 * 2 + 1.11)); document.write('<br>'); document.write('Boolean(1<2) is ' + Boolean(1 < 2)); </script> </body> </html>
Illustration 2
The following program will return false values as its output.
<!DOCTYPE html> <html> <body> <script> var e; //undefined document.write('Boolean("") is ' + Boolean("")); document.write('<br>'); document.write('Boolean(0) is ' + Boolean(0)); document.write('<br>'); document.write('Boolean(-0) is ' + Boolean(-0)); document.write('<br>'); document.write('Boolean(e) undefined is ' + Boolean(e)); document.write('<br>'); document.write('Boolean(false) is ' + Boolean(false)); document.write('<br>'); document.write('Boolean(1>2) is ' + Boolean(1 > 2)); document.write('<br>'); document.write('Boolean(NaN) is ' + Boolean(NaN)); document.write('<br>'); document.write('Boolean(null) is ' + Boolean(null)); </script> </body> </html>
Illustration 3
The boolean values returned by the comparison expressions show if the comparison would be true or false. The expressions illustrated below will return boolean values.
var a = 30, b = 20; var result = 1 > 2; // true result = a < b; // false result = a > b; // true result = a + 10 > b + 5; // true
JS Booleans as Objects
JavaScript booleans are often primitive values derived from literals:
let a = false;
However, booleans may be declared as objects using the keyword new:
let b = new Boolean(false);
Example
let a = false; let b = new Boolean(false); // typeof a returns boolean // typeof b returns object
When the == operator is used, a and b are equal:
let a = false; let b = new Boolean(false);
When the === operator is used, a and b are not equal:
let a = false; let b = new Boolean(false);
Take note of the distinction between (a===b) and a==b).
(a === b)
true of false?
let a = new Boolean(false); let b = new Boolean(false);
(a == b)
true of false?
let a = new Boolean(false); let b = new Boolean(false);
Difference between “Boolean” and “boolean”
In Javascript:
When you write boolean in lower-case, it is primitive.
While, Boolean written in upper-case is a Javascript Object
Example
var x1 = new Boolean(true); var x2 = true; typeof x1; // object typeof x2; // boolean
Javascript Boolean Methods
Method | Description and Examples |
toString() | This Returns a Boolean String Example: var result = (5 > 3); result.toString(); // returns “true” |
valueOf() | This returns a Boolean Object’s value Example: var result = (5 > 3); result.valueOf(); // returns true |
toSource() | The source of the Boolean object is returned as a string. |