This is a set of practical Js
tips, it could also be considered a list of Js
little tricks. Although the following series of operations can make the code more concise to some extent, it will reduce readability in the absence of comments. Therefore, these black magic tricks should be used with caution.
console.log(~~(11.11)); // 11
console.log(11.11 >> 0); // 11
console.log(11.11 << 0); // 11
console.log(11.11 | 0); // 11
console.log(11.11 >>> 0); // 11 // Be aware that >>> cannot be used for rounding negative numbers
console.log(7 & 1); // 1
console.log(8 & 1) ; // 0
console.log(!!7); // true
console.log(!!0); // false
console.log(!!-1); // true
console.log(!!0.71); // true
console.log(!!""); // false
console.log(!![]); // true
console.log(!!{}); // true
console.log(!!null); // false
console.log(!!undefined); // false
console.log(16 >> 1); // 8
console.log(16 << 2); // 64
console.log(1 >>> 1); // 2
let a = 7;
let b = 1;
a ^= b;
b ^= a;
a ^= b;
console.log(a); // 1
console.log(b); // 7
// It can also be done with arrays
b = [a, a = b][0];
// Of course, destructuring assignment is simpler
[a, b] = [b, a];
let a = 1;
let b = 1;
console.log((a ^ b) >= 0); // true
console.log((a ^ -b) >= 0); // false
let a = 111;
if(a ^ 111) console.log("Not equal");
if(a !== 111) console.log("Not equal");
if(a ^ 11111) console.log("Not equal"); // Not equal
const check = n => !(n & (n - 1));
console.log(check(7)); // false
console.log(check(8)); // true
let bool = true;
if(bool) console.log(1); // 1
console.log(bool && console.log(1)); // 1
const round = n => n + 0.5 * (n > 0 ? 1 : -1) | 0;
console.log(round(0)); // 0
console.log(round(1.1)); // 1
console.log(round(1.6)); // 2
console.log(round(-1.1)); // -1
console.log(round(-1.6)); // -2
console.log(Math.random().toString(16).slice(2)); // c21f331e6ce2b
const repeat = (n, str) => Array(n+1).join(str);
console.log(repeat(5, "ab")); // ababababab
console.log("ab".repeat(5)); // ababababab // ES6
console.log("Google".link("www.google.com")); // <a href="www.google.com">Google</a>
try {
// something
} catch (e) {
window.location.href = "http://stackoverflow.com/search?q=[js]+" + e.message;
}
console.log(([][[]]+[])[+!![]]+([]+{})[!+[]+!![]]);
console.log(void 0); // undefined
console.log(""._); // undefined
console.log(1.._); // undefined
console.log(0[0]); // undefined
console.log([-1/0, 1/0]); // [-Infinity, Infinity]
let arr = [1];
arr.length = 0;
console.log(arr); // []
var isIE8 = !+"1";
console.log(isIE8); // false // Chrome 87
https://github.com/WindrunnerMax/EveryDay
https://zhuanlan.zhihu.com/p/150556186
https://zhuanlan.zhihu.com/p/262533240
https://github.com/jed/140bytes/wiki/Byte-saving-techniques