JavaScript double exclamation mark (!!) Introduction to the role example

Source: Internet
Author: User

  This article mainly introduces the double exclamation mark in JavaScript (!!) The role of the need for a friend can refer to the following

Often see such examples:     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 to the appropriate type, like :    Code as follows: A=parseint ("1234″)   a=" 1234″-0//Convert to Digital   b=1234+ ""//Convert to string   c=someobject.tostring ( //Convert object to string     where the 1th and 4th are explicit conversions, 2, 3 for implicit conversions     Boolean conversions, JavaScript convention rules for     false, undefinded, NULL, 0, "" is false  true, 1, "somestring", [Object] for true    null and undefined, and other implicitly converted values, with the! Operator will produce true results, So the effect of using two exclamation points is to convert these values to "equivalent" Boolean values;  look at the:    code as follows: Var foo;  alert (!foo)//undifined case, An exclamation point returns true;  alert (!goo);//null case, 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, the return is false with two exclamation marks, so the effect of the two exclamation points is that If the value of the variable is explicitly set (not null/undifined/0/""Equivalent", the result is returned based on the actual value of the variable, and if there is no setting, the result returns false.    

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.