Last active
October 26, 2020 01:13
-
-
Save sprakash57/124ee72f2c8a3a38b7bf65ba68918860 to your computer and use it in GitHub Desktop.
ES2020 summary
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* 1. BigInt */ | |
conosle.log(typeof 22n); // bigint | |
console.log(25n > 20); // true | |
console.log("1" + 10n); // "11" | |
console.log(34n.toString()); // "34" | |
console.log(2n == 2) //true | |
console.log(2n === 2) //false | |
/* 2. Promise.allSettled() */ | |
// Output will be array of resolved and rejected objects | |
Promise.allSettled([ | |
new Promise(res => setTimeout(() => res(1), 3000)), | |
new Promise((res, rej) => setTimeout(() => rej(new Error("Oops")), 5000)), | |
new Promise(res => setTimeout(() => resolve(3), 1000)) | |
]).then(data => console.log(data)); | |
Output - | |
[ | |
{ status: 'fulfilled', value: 1 }, | |
{ | |
status: 'rejected', | |
reason: Error: oops | |
..... | |
..... | |
}, | |
{ status: 'fulfilled', value: 3 } | |
] | |
// All resolved, otherwise only rejected promise. | |
Promise.all([ | |
new Promise(res => setTimeout(() => res(1), 3000)), | |
new Promise((res, rej) => setTimeout(() => rej(new Error("Oops")), 5000)), | |
new Promise(res => setTimeout(() => resolve(3), 1000)) | |
]).then(data => console.log(data)); | |
/* 3. Nullish Coalescing (??) */ | |
// Strictly checks for undefined. | |
console.log(undefined || "1"); // "1" | |
console.log(undefined ?? "1"); // "1" | |
console.log(0 || "1"); // "1" | |
console.log(0 ?? "1"); // 0 | |
/* 4. Optional chaining (?) */ | |
// Access deeply nested object properties without worrying about reference error. | |
const response = { | |
first: { | |
second: { | |
third: "this you want to access" | |
} | |
} | |
} | |
console.log(first.otherSecond.third); //Reference error | |
console.log(first?.otherSecond?.third); //undefined | |
/* 5. globalThis */ | |
globalThis.log = "This will be accessible everywhere". | |
/* 6. Dynamic import */ | |
//print.js | |
const print = (value) => `Hi ${value}` | |
export { print }; | |
//greet.js | |
const greet = value => import('./print.js).then(func => func(value)); | |
greet("sunny"); //Hi sunny | |
/* 7.String.prototype.matchAll() */ | |
// Replacement of regex.exec(). Returns iterator. | |
const regexp = RegExp('[a-z]*ame','g'); | |
const str = 'rame suot name vjaoi game'; | |
let match; | |
while ((match = regexp.exec(str)) !== null) { | |
console.log(match) | |
} | |
// Same as | |
console.log(...str.matchAll(regexp)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment