By: Syed Fazal in Javascript Tutorials on 2008年08月16日 [フレーム]
The logical AND operator in JavaScript is indicated by the double ampersand ( && ):
Logical AND can be used with any type of operands, not just Boolean values. When either operand is not a primitive Boolean, logical AND does not always return a Boolean value:
var bTrue = true;
var bFalse = false;
var bResult = bTrue && bFalse;
Logical AND behaves as described in the following truth table:
Operand 1 Operand 2 Result
true true true
true false false
false true false
false false false
Just as in Java, logical AND is a short-circuited operation, meaning that if the first operand determines the result, the second operand is never evaluated. In the case of logical AND, if the first operand is false, no matter what the value of the second operand, the result can’t be equal to true. Consider the following example:
var bTrue = true;
var bResult = (bTrue && bUnknown); //error occurs here
alert(bResult); //this line never executes
This code causes an error when the logical AND is evaluated because the variable bUnknown is undefined. The value of variable bTrue is true , so the logical AND operator continued on to evaluate variable bUnknown . When it did, an error occurred because bUnknown is undefined and, therefore, cannot be used in a logical AND operation. If this example is changed so that a is set to false , the error won’t occur:
var bFalse = false;
var bResult = (bFalse && bUnknown);
alert(bResult); //outputs “false”
In this code, the script writes out the string “false” , the value returned by the logical AND operator. Even though the variable bUnknown is undefined, it never gets evaluated because the first operand is false . You must always keep in mind short-circuiting when using logical AND.
This policy contains information about your privacy. By posting, you are declaring that you understand this policy:
This policy is subject to change at any time and without notice.
These terms and conditions contain rules about posting comments. By submitting a comment, you are declaring that you agree with these rules:
Failure to comply with these rules may result in being banned from submitting further comments.
These terms and conditions are subject to change at any time and without notice.
Most Viewed Articles (in Javascript )
Dynamically modify the option set in Dynamics 365 forms
promise and .then() in JavaScript
reduce() and filter() in JavaScript
History and evolution of Javascript
Using parseInt() and parseFloat() in JavaScript to convert data types to Numbers
Show how many characters remaining in a html text box using javascript
Latest Articles (in Javascript)
© 2023 Java-samples.com
Tutorial Archive: Data Science React Native Android AJAX ASP.net C C++ C# Cocoa Cloud Computing EJB Errors Java Certification Interview iPhone Javascript JSF JSP Java Beans J2ME JDBC Linux Mac OS X MySQL Perl PHP Python Ruby SAP VB.net EJB Struts Trends WebServices XML Office 365 Hibernate
Latest Tutorials on: Data Science React Native Android AJAX ASP.net C Cocoa C++ C# EJB Errors Java Certification Interview iPhone Javascript JSF JSP Java Beans J2ME JDBC Linux Mac OS X MySQL Perl PHP Python Ruby SAP VB.net EJB Struts Cloud Computing WebServices XML Office 365 Hibernate