Using && in Javascript

By: Syed Fazal Emailed: 1672 times Printed: 2153 times    

Latest comments
By: rohit kumar - how this program is work
By: Kirti - Hi..thx for the hadoop in
By: Spijker - I have altered the code a
By: ali mohammed - why we use the java in ne
By: ali mohammed - why we use the java in ne
By: mizhelle - when I exported the data
By: raul - no output as well, i'm ge
By: Rajesh - thanx very much...
By: Suindu De - Suppose we are executing

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:

  • If one operand is an object and one is a Boolean, the object is returned.
  • If both operands are objects, the second operand is returned.
  • If either operand is null , null is returned.
  • If either operand is NaN , NaN is returned.
  • If either operand is undefined , an error occurs.

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.


Javascript Home | All Javascript Tutorials | Latest Javascript Tutorials

Sponsored Links

If this tutorial doesn't answer your question, or you have a specific question, just ask an expert here. Post your question to get a direct answer.



Bookmark and Share

Comments(0)


Be the first one to add a comment

Your name (required):


Your email(required, will not be shown to the public):


Your sites URL (optional):


Your comments:



More Tutorials by Syed Fazal
switch in Javascript
break and continue in Javascript
do-while, while, for and for-in loops in Javascript
The if statement in Javascript
Using && in Javascript
Using ! and !! in Javascript
Bitwise NOT, AND, OR, XOR operators in Javascript
String Concatenation using Java

More Tutorials in Javascript
Use WinSCP to transfer log files remotely using Javascript
Verifying user input in JavaScript
Pause execution in JavaScript
Javascript to display client date and time on webpage
Getting Browser's height and width using Javascript
Highlighting text on a page using CSS
Using revealTrans to do page transitions in Javascript
Password protect a web page using Javascript
Diabling Right Click option in a browser using Javascript
Scrolling message on the status bar using Javascript
window.frames[i] in Javascript
Form validation using Javascript
Using && in Javascript
The if statement in Javascript
do-while, while, for and for-in loops in Javascript

More Latest News
Most Viewed Articles (in Javascript )
List of Keywords and reverved words in 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
Typecasting in Javascript
Using toString() in JavaScript to convert data types to String
Using && in Javascript
Getting Browser's height and width using Javascript
Click to remove an element from a select html form using javascript
History and evolution of Javascript
Javascript Basics
Bitwise NOT, AND, OR, XOR operators in Javascript
Using ! and !! in Javascript
The if statement in Javascript
do-while, while, for and for-in loops in Javascript
break and continue in Javascript
Most Emailed Articles (in Javascript)
window.frames[i] in Javascript
Scrolling message on the status bar using Javascript
Verifying user input in JavaScript
List of Keywords and reverved words in Javascript
Typecasting in Javascript
The if statement in Javascript
call() and apply() methods in Javascript
Form validation using Javascript
Pause execution in JavaScript
Javascript Basics
Using parseInt() and parseFloat() in JavaScript to convert data types to Numbers
do-while, while, for and for-in loops in Javascript
this keyword sample in Javascript
Password protect a web page using Javascript
Javascript to display client date and time on webpage