Special JavaScript statements and usage

Source: Internet
Author: User

JavaScript is a commonly used scripting language in front-end development. I have sorted out some rare usage and supplemented it after being used for drying. If any of the errors is correct, please kindly advise.


1. closure, which can avoid variable contamination. Many js frameworks (such as jQuery) and plug-ins use the closure method for implementation.
(Function (args1, args2. ..) {// js code}) (args1, args2 ...);
Equivalent Syntax :! Function () {// js code} (); // This function is not recommended and may have compatibility issues.


2. Comma expression. Multiple statements can be combined into one statement.
Return alert ('ha ha! '),! 1; // comma expression, the value is the value of the last expression ,! 1 indicates false, and the data type is boolean;


3. Forcibly use the Boolean Type (! Non-zero: false ,! 0 indicates true)
Var a1 = 1, b1 = 0;
Var c1 = a1 | b1;
Alert (typeof a1 + "," + typeof b1 + "," + typeof c1); // all types are numeric


Var a2 =! 1, b2 =! 0;
Var c2 = a2 | b2;
Alert (typeof a2 + "," + typeof b2 + "," + typeof c2); // all types are Boolean


4. Use void 0 (equivalent to undefined)
Var a = void 0; // void 0 is undefined, which has strong compatibility.
Var B = undefined; // The undefined value can be overwritten. If undefined = 'abc' is added, B is equal to 'abc'

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.