Double exclamation point in JavaScript !! Function example introduction, javascript example

Source: Internet
Author: User

Double exclamation point in JavaScript !! Function example introduction, javascript example

!! It is generally used to forcibly convert the following expression to a boolean data (boolean), that is, it can only be true or false;

We often see this example:
Copy codeThe Code is as follows:
Var;
Var B = !! A;

A is undefined by default .! A is true ,!! A is false, so the value of B is false, not undefined or another value, which is mainly to facilitate subsequent judgment.

!! It is generally used to forcibly convert the following expression to a boolean data (boolean), that is, it can only be true or false;
Javascript is a weak type language (the variable does not have a fixed data type), so sometimes it needs to be forcibly converted to the corresponding type, similar:

A = parseInt ("1234") a = "1234"-0 // convert to number B = 1234 + "// convert to string c = someObject. toString () // convert an object to a string

Among them, 1st and 4th are Explicit conversions, and 2 and 3 are implicit conversions.

Boolean conversion. The javascript Convention rule is

False, undefinded, null, 0, "is false

True, 1, "somestring", [Object] is true

For other values such as null and undefined that are implicitly converted, use! The true result is generated when the operator is used. Therefore, the function of Using Two exclamation points is to convert these values into "equivalent" Boolean values;

Let's take a look:

Var foo; alert (! Foo); // In the case of undifined, true is returned for an exclamation point; alert (! Goo); // null, true is returned for an exclamation point; var o = {flag: true}; var test = !! O. flag; // equivalent to var test = o. flag | false; alert (test );

In this example, when undifined and null are used, true is returned with an exclamation point, and false is returned with two exclamation points. Therefore, the role of the Two exclamation points is, if the variable value is explicitly set (non-null/undifined/0/"" equivalent), the result is returned based on the actual value of the variable. If no value is set, the result returns false.


Javascript exclamation point

!! It is generally used to convert the following expression to a boolean data (boolean)
Javascript is a weak type language (the variable does not have a fixed data type), so sometimes it needs to be forcibly converted to the corresponding type, similar:
A = parseInt ("1234 ")
A = "1234" + 0 // convert to a number
B = 1234 + "// convert to string
C = someObject. toString () // convert the object to a string
Among them, 1st and 4th are Explicit conversions, and 2 and 3 are implicit conversions.

The javascript Convention is similar to c, and the rule is
· False, undefinded, null, 0, and "" are false.
· True, 1, "somestring", and [Object] are true

<! -- // --> Function

You mean
<Script>
<! --
Function xx () {// code}
-->
</Script>
In the early days, Some browsers did not support JavaScript, so JavaScript code is considered as text and directly displayed on the page, so we use the page Comment Method <! --> Set the js Code so that even if the browser does not support js, the code will not be directly displayed on the page. If the browser supports JavaScript, <! --> This Is Not A js annotation method and does not affect the execution of js Code. Most browsers now support JavaScript. Even if JavaScript is disabled manually, there is now a <noscript> label to deal with this situation.

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.