What does JS pre-plus semicolon and exclamation mark mean?

Source: Internet
Author: User

;!  function () {} (); ;! What's the use?

From the syntax, the semicolon in JavaScript indicates the end of the statement, and at the beginning, it may be used for compression and other methods to split, indicating a new statement begins. So, if in a separate JS file, the beginning of the semicolon is meaningless, can be deleted.

An exclamation point is a logical operator, a "non", a common use of the IF (!true) {}, whereas the operator is preceded by the function definition as a whole, then the function is called, and the returned structure is logically calculated.

Plainly, here is the building of an immediate execution function, it is recommended that the landlord know the meaning can be, when they write, you can use a clearer format:

(function () {}) ();

Add a point here: Add "()" after the function Declaration "functions () {}", meaning that the function declaration is executed. Some are called "anonymous auto-execute functions" and more accurately represent "execute functions immediately".

But execute the function immediately, as part of the expression, or the JS interpreter will report a syntax error. In general, the easiest way to understand an expression is to add a parenthesis to the left and right ends, such as "(function () {})". Therefore, the most common notation for executing functions immediately is "(function () {})", which is a statement that can be added "before and after"; 】。

In fact, the immediate execution of the function is also a n-type, such as "(function () {})" is also OK, before the function declaration with a unary operator also line: "!function () {} ()", "~function () {} ()". Even a combination of unary operators can: "!!!! function () {} () "," ~~~function () {} () ","!~!function () {} () ").

Recommended reading Uncle Tom's "in-depth understanding of the JavaScript series"

What does JS pre-plus semicolon and exclamation mark mean?

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.