JavaScript uses two logical non-operators (!!) The cause

Source: Internet
Author: User

JavaScript uses two logical non-operators (!!) Reasons for:
About the use of logical non-operators it's easy to say, if you don't know the logical operators section of JavaScript .
In some code it may be noted that some places use two logical non-operators, the first feeling is not necessary, such as the operand is true, the use of two logical non-return value or true, as if superfluous behavior, in fact, if it is not so.
Let's look at a code example:

grep:function(ELEMS,CALLBACK,INV) {   var ret=[],retval;   INV=!! INV;     for (var i=0,length=elems.length;i<length;i++ ) {     retVal=!! Callback (elems[/size][i][size=2],i);     if (inv!==retVal) {       Ret.push (elems[    }   }   return  ret;}

The above code is a definition of the grep () function in the jquery source. The purpose of this is to be able to return false when the operand is null or undefined, rather than null or undefined, which makes it easier to do something. The above code, the third parameter can be omitted, when omitted can be used!! operator to convert undefined to false.

The original address is: http://www.softwhy.com/forum.php?mod=viewthread&tid=9118

For more information, refer to: http://www.softwhy.com/javascript/

JavaScript uses two logical non-operators (!!) The cause

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.