NOT Operator (!)

By: Dusty Arlia
Published on October 31, 2012
Last Updated on Sunday, July 12, 2015 at 3:24 PM
Total Updates: 5

The NOT operator represented by an exclamation mark (!) is often used to execute some code based on a negated condition. If the condition was true, the ! operator makes it false. If the condition was false, the ! operator makes it true. Here is an example how the ! operator is used:

if (!validForm) {
alert('Error: Please recheck your form data.');
} 

The first line of this example says, "if validForm is not true, execute this next statement". validForm is a variable that can have a Boolean value of true or false based on the user input. If the user forgets to fill out a form field, you can set validForm to the value false.

When you use two ! operators in a row, you get the Boolean value of that expression. Here's an example:

!!true_variable     //evaluates to true
!!false_variable    //evaluates to false

The reason for getting these true and false values is because we are negating these variables twice.

When you use the NOT operator with the equal sign, you get the inequality operator (!=).

Comments:

Ad: