Buy traffic for your website
  • Save

Boolean Operators in JavaScript

Boolean Operators in JavaScript
  • Save

Introduction to Boolean Operators in JavaScript

Java script is a frontend development Language that has been used in websites for long. While there are several back end languages from programmers to choose from, there has been no alternative language developed as a replacement of Java script. This has been due to its ability to manage HTML components, CSS styling, and data altogether.

Understanding Boolean Operators

There are three operators: AND, OR and NOT.  They may be used either in a database or in coding and come very hand to developers when building components of a complex logic or flow.

How to Implement Boolean Operators in JavaScript?

Let’s understand a bit more about each of these implemented in Javascript with more details.

1. OR Operator in JavaScript

The OR operator in Javascript is represented in symbolic form with two vertical lines. For instance, let’s consider the below example:

Syntax

result = x || y; // same as x OR y

In a typical programming language, the Boolean or logical OR operator is used to evaluate multiple Boolean variables only. If any of its variables provided are true then the expression evaluates and returns true else it would return a false value. Java script has a very powerful implementation of OR operators. Let’s understand the application of OR logical operator better with the help of an example.

Basically there would be four logical combinations of Boolean values listed below :

True || Truei.e. True “OR” True// the result will be True False || True i.e False“OR” True// the result will be True True || False i.eTrue “OR”False// the result will be True False || False i.eFalse“OR”False// the result will be True

As shown above, the result of this operation will always be true unless one of the operands is false. Let’s see the implementation of the OR operator in the below example:

Code:

<!DOCTYPE html> <html> <body> <h2>Java script OR operator </h2> <p>This example is for OR operator </p> <p id=”demo”></p> <script> if ( true || false ) { // works just like if( true || false ) document.getElementById(“demo”).innerHTML = “truthy”; } </script> </body> </html>

Output:

Boolean Operators in JavaScript - 1
  • Save

2. AND Operator in Javascript symbolized as &&

The AND operator in Javascript is represented in symbolic form with two ampersands &&.

Syntax

var result = x && y; // equivalent to x AND y

Like the OR operator, the Boolean or logical AND operator is used to evaluate multiple Boolean operands. If any of its variables provided are false then the expression evaluates and returns false else it would return a true value. Let’s understand the application of AND logical operator better with the help of an example.

Basically, with AND operator, there would be four logical combinations of Boolean values listed as follows:

True || True i.e. True “OR” True // the result will be True False || True i.e False“OR” True // the result will be False True || False i.e True “OR” False // the result will be False False || False i.e False“OR” False // the result will be False

As shown above, the result of this operation will always be false unless both of the operands are true. Let’s see the implementation of the AND operator in the below example:

Code:

<!DOCTYPE html> <html> <body> <h2>Java scriptAND operator </h2> <p>This example is for AND operator </p> <p id=”demo1″></p> <p id=”demo2″></p> <script> if ( true && false ) { document.getElementById(“demo1”).innerHTML = “false value”; } if ( true && true ) { document.getElementById(“demo2”).innerHTML = “true value”; } </script> </body> </html>

Output:

Boolean Operators in JavaScript - 2
  • Save

3. NOT Operator in JavaScript Symbolized as !

The NOT operator in Javascript is represented in symbolic form with an exclamationmark&&.

Syntax

var result = ! y;

Like the OR and AND operator, the Boolean or logical ANDoperator is used to evaluate multiple Boolean operands only. It is used to inverse the value of the operand since it returns the opposite value of the operand provided to it. Let’s understand the application of a NOT logical operator better with the help of an example.

Basically, with NOT operator there would be 2 logical possibilities of Boolean values which are listed below:

! True i.e. equivalent toNOT True // the final result will be a False boolean value since it is opposite of true ! False i.e. equivalent to NOT False // the final result will be a True Boolean value since it is opposite of False

As shown above, the result of this operation will always the opposite of the operand on which the operation has been applied. Let’s see the implementation of the NOT operator in the below example:

Code:

<!DOCTYPE html> <html> <body> <h2>Java scriptNOT operator </h2> <p>Thisexample is for NOT Boolean operator </p> <p id=”demo1″></p> <p id=”demo2″></p> <script> if ( true || false ) { var result = false; // defining variable as false document.getElementById(“demo1”).innerHTML = ! result ; //variable output here would be true } if ( true && true ) { var result = true; // defining variable as true document.getElementById(“demo2”).innerHTML = ! result ; //variable output here would be false }</script> </body> </html>

Output:

Boolean Operators in JavaScript - 3
  • Save

Conclusion

From the above description, we can understand the significance of Boolean operators in a programming language. We can use the AND, OR and NOT operators in database queries as well as when developing complex logic that would require condition verification and conditional behavior implemented with if or while.

Recommended Articles

This is a guide to Boolean Operators in JavaScript. Here we discuss the significance of Boolean operators, by using AND, OR and NOT operators. You can also go through our other related articles to learn more –

JavaScript String to Number JavaScript Get Element by Class JavaScript Style visibility JavaScript String to Float

The post Boolean Operators in JavaScript appeared first on EDUCBA.

Read more: educba.com

Leave a Comment

Share via
Copy link
Powered by Social Snap