- ES6 Tutorial
- ES6 - Home
- ES6 - Overview
- ES6 - Environment
- ES6 - Syntax
- ES6 - Variables
- ES6 - Operators
- ES6 - Decision Making
- ES6 - Loops
- ES6 - Functions
- ES6 - Events
- ES6 - Cookies
- ES6 - Page Redirect
- ES6 - Dialog Boxes
- ES6 - Void Keyword
- ES6 - Page Printing
- ES6 - Objects
- ES6 - Number
- ES6 - Boolean
- ES6 - Strings
- ES6 - Symbol
- ES6 - New String Methods
- ES6 - Arrays
- ES6 - Date
- ES6 - Math
- ES6 - RegExp
- ES6 - HTML DOM
- ES6 - Iterator
- ES6 - Collections
- ES6 - Classes
- ES6 - Maps And Sets
- ES6 - Promises
- ES6 - Modules
- ES6 - Error Handling
- ES6 - Object Extensions
- ES6 - Reflect API
- ES6 - Proxy API
- ES6 - Validations
- ES6 - Animation
- ES6 - Multimedia
- ES6 - Debugging
- ES6 - Image Map
- ES6 - Browsers
- ES7 - New Features
- ES8 - New Features
- ES9 - New Features
- ES6 Useful Resources
- ES6 - Quick Guide
- ES6 - Useful Resources
- ES6 - Discussion
ES6 - Logical operators
Assume the value of variable A is 10 and B is 20.
Example
var avg = 20; var percentage = 90; console.log("Value of avg: " + avg + " ,value of percentage: " + percentage); var res = ((avg > 50) && (percentage > 80)); console.log("(avg>50)&&(percentage>80): ", res); var res = ((avg > 50) || (percentage > 80)); console.log("(avg>50)||(percentage>80): ", res); var res = !((avg > 50) && (percentage > 80)); console.log("!((avg > 50)&&(percentage>80)): ", res);
The following output is displayed on successful execution of the above code.
Value of avg: 20 ,value of percentage: 90 (avg > 50)&&(percentage > 80): false (avg > 50)||(percentage > 80): true !((avg > 50)&&(percentage > 80)): true
Short-circuit Operators
The && and || operators are used to combine expressions.
The && operator returns true only when both the conditions return true. Let us consider an expression −
var a = 10 var result = ( a<10 && a>5)
In the above example, a<10 and a>5 are two expressions combined by an && operator. Here, the first expression returns false. However, the && operator requires both the expressions to return true. So, the operator skips the second expression.
The || operator returns true, if one of the expressions return true. For example −
var a = 10 var result = ( a>5 || a<10)
In the above snippet, two expressions a>5 and a<10 are combined by a || operator. Here, the first expression returns true. Since, the first expression returns true, the || operator skips the subsequent expression and returns true.
Due to this behavior of the && and || operator, they are called as short-circuit operators.