JAVASCRIPT BOOLEAN
The boolean in javascript is a primitive data type that has two values true
representing yes and false
representing no.
Boolean is generally used in conditional statements or at places when we have to control the flow example loops, if-else, switch, etc.
Example
// Boolean
var positive = true;
var negative = false;
console.log(typeof positive);
console.log(typeof negative);
//String
var x = "true";
var y = "false";
console.log(typeof x);
console.log(typeof y);
Try it
Boolean Constructor Function
The Boolean() constructor function converts the value passed to it to a boolean value.
The Boolean()
function can also convert javascript expressions to boolean values.
Example
var positive = Boolean(true);
var negative = Boolean(false);
console.log(typeof positive);
console.log(typeof negative);
var x = Boolean(10 > 20);
var y = Boolean(15 === 15);
var z = Boolean(20 < 30);
console.log(x, y, z);
Try it
Note: If the value passed is 0, undefined, null, NaN, empty string, or false, the Boolean()
function will return false and if the value passed is any other value, the Boolean()
function will return true.
Let's see how the Boolean()
function works with some examples.
Example
console.log("0 is " + Boolean(0));
console.log("1 is " + Boolean(1));
console.log("-15 is " + Boolean(-15));
console.log("1 < 2 is " + Boolean(1 < 2));
console.log("25 === 5*5 is " + Boolean(25 === 5*5));
console.log("Any valid string returns " + Boolean("Some string"));
console.log("Empty string is " + Boolean(''));
console.log("All object returns " + Boolean([]));
console.log("All object returns " + Boolean({}));
console.log("undefined is " + Boolean(undefined));
console.log("null is " + Boolean(null));
console.log("NaN is " + Boolean(NaN));
Try it
Boolean as Object
Generally Boolean is a primitive data type when created using literals. Example var bool = true;
.
But booleans can be made objects using new
keywords. Example var boolObj = new Boolean(true);
.
Example
var bool = true; // boolean
var boolObj = new Boolean(true); // object
console.log(typeof bool);
console.log(typeof boolObj);
Try it
Note: Use literals to define boolean because boolean as the object is slow.
JavaScript String To Boolean
To convert a string to boolean, we can use the Boolean()
function or you can use the negation operator !
.
Negation (!
) operator is used to convert a value to its opposite value. Example !true
will return false and !false
will return true.
To convert a string to boolean use negation operator it will convert it to boolean but opposite value so use another negation operator to convert it to its original value. Example !!'Hello world'
will return true.
Example
var str1 = "Hello world";
var str2 = "";
// negation operator
console.log(!!str1);
console.log(!!str2);
// Boolean() function
console.log(Boolean(str1));
console.log(Boolean(str2));
Conclusion
Boolean is the truthy value of any type of data in javascript. It can be either true or false.
Booleans can be converted to string using Boolean()
function or using negation operator !
.