Home >Web Front-end >JS Tutorial >JavaScript special statements and usage
JavaScript is a scripting language commonly used in front-end development. I have compiled some rare usages
1. Closures, which have the advantage of avoiding variable pollution. Many js frameworks (such as jQuery) and plug-ins use closures to implement
(function(args1, args2...){ //js code})(args1, args2...);
Equivalent writing: !function(){ //js code}(); // Not recommended , there may be compatibility issues
2. Comma expression, you can combine multiple statements into one statement
Return alert('ha ha!'),!1; //Comma expression, the value is the last expression The value of || b1;
var a2 = !1, b2 = !0;
var c2 = a2 || b2;
alert(typeof a2 + "," + typeof b2 + "," + typeof c2); // The types are all Boolean types
4, clever use of void 0 (equivalent to undefined)
var a = void 0; // void 0 is undefined, with strong compatibility
var b = undefined; // The undefined value can be overwritten. If undefined = 'abc' is added, b is also equal to 'abc'