Using ! and !! in Javascript

By: Syed Fazal Emailed: 1730 times Printed: 2341 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 NOT operator in JavaScript is the same as in C and Java, indicated by an exclamation point ( ! ). Unlike logical OR and logical AND operators, the logical NOT always returns a Boolean value.

The logical NOT operator behaves in the following way:

  • If the operand is an object, false is returned.
  • If the operand is the number 0, true is returned.
  • If the operand is any number other than 0, false is returned.
  • If the operand is null , true is returned.
  • If the operand is NaN , true is returned.
  • If the operand is undefined , an error occurs.

This operator is typically used in control loops (discussed later):

var bFound = false;
var i = 0;
while (!bFound) {

if (aValues[i] == vSearchValue) {
    bFound = true;
} else {
    i++;
}

}

In this example, a Boolean variable ( found ) keeps track of the success of a search. When the item in question is located, found is set to true , which causes !found to equal false , meaning that execution will escape the while loop.

The logical NOT operator is also useful in determining the Boolean equivalent of an JavaScript variable. In order to do this, you use two logical NOT operators in a row. The first NOT returns a Boolean value no matter what operand it is given. The second NOT negates that Boolean value and so gives the true Boolean value of a variable.

var bFalse = false;
var sBlue = “blue”;
var iZero = 0;
var iThreeFourFive = 345;
var oObject = new Object;

document.write(“The Boolean value of bFalse is “ + (!!bFalse));
document.write(“<br />The Boolean value of sBlue is “ + (!!sBlue));
document.write(“<br />The Boolean value of iZero is “ + (!!iZero));
document.write(“<br />The Boolean value of iThreeFourFive is “ + (!!iThreeFourFive));
document.write(“<br />The Boolean value of oObject is “ + (!!oObject));

Running this example yields the following output:

The Boolean value of bFalse is false

The Boolean value of sBlue is true

The Boolean value of iZero is false

The Boolean value of iThreeFourFive is true

The Boolean value of oObject is true


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 )
Typecasting in Javascript
Bitwise NOT, AND, OR, XOR operators in Javascript
Using parseInt() and parseFloat() in JavaScript to convert data types to Numbers
Javascript Basics
Using toString() in JavaScript to convert data types to String
Show how many characters remaining in a html text box using javascript
List of Keywords and reverved words in Javascript
Using ! and !! in Javascript
Using && in Javascript
Password protect a web page using Javascript
Verifying user input in JavaScript
Pause execution in JavaScript
Use WinSCP to transfer log files remotely using Javascript
Click to remove an element from a select html form using javascript
History and evolution of Javascript
Most Emailed Articles (in Javascript)
Password protect a web page using Javascript
Show how many characters remaining in a html text box using javascript
Javascript Basics
Using parseInt() and parseFloat() in JavaScript to convert data types to Numbers
Using ! and !! in Javascript
switch in Javascript
window.frames[i] in Javascript
Form validation using Javascript
Using revealTrans to do page transitions in Javascript
Scrolling message on the status bar using Javascript
Use WinSCP to transfer log files remotely using Javascript
Click to remove an element from a select html form using javascript
History and evolution of Javascript
List of Keywords and reverved words in Javascript
Typecasting in Javascript