JavaScript double exclamation mark (!!) Action Sample Introduction to _javascript Tips

Source: Internet
Author: User
Often see examples of this:
Copy Code code as follows:

var A;
var b=!! A

A default is undefined.!a is true,!! A is false, so the value of B is false, not undefined, and not other values, primarily for subsequent judgment.

!! Typically used to cast a subsequent expression to a Boolean type of data (Boolean), which is only true or false;
Because JavaScript is a weakly typed language (a variable has no fixed data type), it is sometimes necessary to cast it to the appropriate type, like this:
Copy Code code as follows:

A=parseint ("1234″")
A= "1234″-0//Convert to Number
b=1234+ ""///Convert to String
C=someobject.tostring ()//Convert an object to a string

Where the 1th and 4th types are explicit conversions, 2, 3 are implicit conversions

Boolean conversions, JavaScript Convention rules for

False, undefinded, NULL, 0, False
True, 1, "somestring", [Object] is True

For other implicitly-converted values, such as null and undefined, the! Operator yields true results, so the two exclamation points are used to convert these values to "equivalent" Boolean values;
Take another look:
Copy Code code as follows:

var foo;
alert (!foo);//undifined case, an exclamation point returns true;
alert (!goo);//null, an exclamation point returns true;
var o={flag:true};
var test=!! o.flag;//equivalent to Var test=o.flag| | False
alert (test);

This example shows that when undifined and Null are returned with an exclamation point that is true and returns false with two exclamation marks, the effect of the two exclamation point is that if the value of the variable (not null/undifined/0/"equivalent) is explicitly set, The result is returned based on the actual value of the variable, and if there is no setting, the result returns false.
Related Article

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.