JavaScript - Boolean




JavaScript - Boolean

Boolean logic is something used in most programming languages, including JavaScript. It's very useful to know at least a little bit about it. In JavaScript, you mostly need it in if() statements. this is only a quick and dirty explanation written for new programmers. In addition, it wholly centers on JavaScript uses of Boolean logic. I first discuss the basic theory, then introduce AND, OR and NOT in JavaScript. Then I present a Boolean generator with which you can make your own Boolean statements and see how they evaluate. after that I explain when JavaScript makes a variable false and finally how to see if an object exists. When you've understood all this you can work with Boolean logic in JavaScript.

The JavaScript Boolean object is the data type for boolean values(true or false). Boolean values are used by programmers of many languages as a way in which they can toggle between true and false in a variable or evaluate expressions. Many expressions in Javascript will result in a boolean value which you can then evaluate using condition logic. You do not have to use the "new Boolean()" constructor syntax and it is said to be more efficient NOT to use the constructor when establishing boolean objects.

// Booleans with false value
var a = false;
var b = Boolean(); 
var c = Boolean(false);
var d = new Boolean(false);
var e = new Boolean(null);
var f = new Boolean("");
var g = new Boolean(0);
// Booleans with true value
var h = true;
var i = Boolean(true);
var j = new Boolean(true);
var k = new Boolean(1);
var l = new Boolean("poop");

All of the approaches shown above will establish Boolean objects in Javascript with initial values of either true or false. Example demonstrating how to evaluate Boolean objects using an if condition -

var a = false;
if(a){
    document.write("a is "+a+", run code"); // "a" must be true for code to run
}

What is an JavaScript Boolean ?

Boolean is a datatype that returns either of two values i.e. true or false. In JavaScript, boolean is used as a function to get the value of a variable, object, conditions, expressions etc. in terms of true or false.

The Boolean object is an object wrapper around the boolean value true or false. This Boolean object type exists primarily to provide a toString() method to convert boolean values to strings. To learn more about the Boolean, please check out the JavaScript data types chapter.

Boolean Properties

The following table lists the standard properties of the Boolean object.

For Example
Property Description
prototype Allows you to add new properties and methods to a Boolean object.

The Boolean type has two literal values: true and false. These values are not numeric values. These values are distinct from numeric values, so true is not equal to 1, and false is not equal to 0. true and false are case-sensitive, so True and False are valid as identifiers but not as Boolean values. The following code creates two boolean type variables:

Example

var isOK = true;
console.log(isOK); 
var isNotOK = false;
console.log(isNotOK); 

The code above generates the following result.

true
false

Boolean is a primitive data type in JavaScript. Boolean can have only two values, true or false. It is useful in controlling program flow using conditional statements like if..else, switch, while, do..while.

Example

var YES = true;

var NO = false;

Output

Demo: JavaScript Boolean

The following example demonstrates how a Boolean value controls the program flow using if condition.

Example

var YES = true;
var NO = false;

if(YES)
{
    alert("This code block will be executed");
}

if(NO)
{
    alert("This code block will not be executed");
}

Output

Demo: JavaScript Boolean