Explain JavaScript logical not operator _javascript tips

Source: Internet
Author: User
Tags arithmetic

In JavaScript, the logical NOT operator is the same as the logical not operator in C and Java, and is represented by an exclamation point (!). Unlike a logical OR and a logical AND operator, the logical NOT operator returns a Boolean value.

the logical NOT operator behaves as follows:

    • Returns False if the OP count is an object
    • If the OP count is number 0, return True
    • If the OP count is any number other than 0, return false
    • Returns True if the OP count is null
    • Returns True if the OP count is NaN
    • If the OP count is undefined, there's a mistake.

The test script is as follows:

<script type= "Text/javascript" > var bfalse = false;//op arithmetic is the number of bool type var sred = "Red";/op arithmetic is string var Izero = 0;// Op count is 0 var ithreefourfive = 345;//arithmetic is 0 other than any number var oobject = new Object ();//object var onull=null;//arithmetic is null var ound efined;//is the undifined var onan=parseint ("abc");//Use the Parseint method to convert the attempt string abc to an integer, because ABC is not a number and therefore cannot be turned, so the result of the return is Nan/* WRI The Teln () method is almost the same as the write () method, except that the former will add a newline character after any supplied string.
  In HTML, this usually only produces a space later, but if the <PRE> and <XMP> identities are used, the line break is interpreted and displayed in the browser.
 * * Document.writeln ("<XMP>");
 Document.writeln ("Onan=parseint" (\ abc\) Returns the result is "+onan");
 Document.writeln (The result of the number of types BOOL and! Operator operations is: "+ (!bfalse));
 The result of Document.writeln ("String sred and! Operator operations is:" + (!sred)); Document.writeln (The result of the number 0 and! Operator operation is: "+ (!izero));//If the arithmetic is number 0, return True Document.writeln (the result of the number 345 and! Operator operation is:" + (!) ithreefourfive);//If the OP count is any number other than 0, return false Document.writeln ("The result of the object Oobject and! Operator operation is:" + (!oobject)); If the OP count is an object, return False Document.writeln (the result of the Nan and! Operator operation is: "+ (!onaN); Returns True Document.writeln (the result after null and! Operator operation is: "+ (!onull));//If the shipping count is null, return True Document.writeln (" und
 Ifined and! The result of the operator operation is: "+ (!oundefined)); 
 The result of Document.writeln ("Undefined string sbule and! Operator operations is:" + (!sbule));//sbule there is no definition before, that is, Sbule is the undefined, so there is an error here.
 Document.writeln ("</XMP>"); </script>

Run Result:

  

You can also use the logical not operator when judging a Boolean value for a JavaScript variable. Doing so requires the use of two not operators in one line of code. Regardless of the type of OP count, the first not operator returns a Boolean value, and the second not negates the Boolean value, giving the variable a true boolean value. It is a useful technique to use the NOT operator to determine the Boolean value of a JavaScript variable, as long as you know the Boolean value of the variable, then use the variable for && or | | Operation, you can quickly know the result of the operation.
The test script is as follows:

<script type= "Text/javascript" > var bfalse = false;//op arithmetic is the number of bool type var sred = "Red";/op arithmetic is string var Izero = 0;// Op count is 0 var ithreefourfive = 345;//arithmetic is 0 other than any number var oobject = new Object ();//object var onull=null;//arithmetic is null var ound
 efined;//is the undifined var onan=parseint ("abc");//Use the Parseint method to convert the attempt string abc to an integer, because ABC is not a number and therefore cannot be turned, so the result of the return is Nan/* You can also use the logical not operator when judging a Boolean value for a JavaScript variable.
 Doing so requires the use of two not operators in one line of code. The first not operator returns a Boolean value, regardless of the type of OP count.
 The second not negates the Boolean value, giving the true Boolean value of the variable.
 * * document.write ("<PRE>"); Document.writeln (The logical value of the Boolean false is "+" (!!).
 Bfalse)); Document.writeln ("The logical value of the string sred is" + (!!)
 sred)); Document.writeln ("The logical value of the number 0 is" + (!!)
 Izero)); Document.writeln ("The logical value of the number 345 is" + (!!)
 ithreefourfive)); Document.writeln ("The logical value of Object objects is" + (!!)
 Oobject)); Document.writeln ("Nan's logical value is:" + (!!)
 ONaN)); Document.writeln ("Null logical value is" + (!!)
 Onull)); Document.writeln ("Undefined logical value is" + (!!)
 oundefined));
 document.write ("</PRE>"); </script>

The above is about the JavaScript logical not operator details, I hope to help you learn.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.