Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save ronit-mukherjee/8d6bbdd68dc82ab2f89fcdd1d1555589 to your computer and use it in GitHub Desktop.
Save ronit-mukherjee/8d6bbdd68dc82ab2f89fcdd1d1555589 to your computer and use it in GitHub Desktop.
JS New Features declared at Google IO 2019
/**
* 1. String.matchAll()
* The matchAll() method returns an iterator of all results matching a string against a
* regular expression, including capturing groups.
*
* URL - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/matchAll
*/
/**
* 2. Numeric Separtor
* http://2ality.com/2018/02/numeric-separators.html
* http://exploringjs.com/es2016-es2017/ch_tc39-process.html
*/
1_234_567_890_123_456_789n
/**
* 3. BigInt
* https://tc39.github.io/proposal-bigint/#sec-bigint.prototype.tolocalestring
*/
//Example why BigInt is Useful
1234567890123456789 * 123
// Output - 151851850485185200000 X
1234567890123456789n * 123n
// Output - 151851850485185185047n
12345678901234567890n.toLocaleString("en");
// Output - 12,345,678,901,234,567,890
const formatter = new Intl.NumberFormat('en');
formatter.format(12_345_678_901_234_567_890n);
// Output - 12,345,678,901,234,567,890
/**
* 4. Async/Await
* Top Level Await
*/
async function test() {
const result = await doAsync();
// Use result here
}
// No use of async as per google proposal
const result = await doAsync();
/**
* 5. Promise.allSettled & Promise.any
*/
const promises = [
asyncFunc1(),
asyncFunc2(),
asyncFunc3()
]
try {
const result = await Promise.all(promises);
doSomething(result);
} catch (error) {
showError(error);
}
try {
const result = await Promise.race(promises);
doSomething(result);
} catch (error) {
showError(error);
}
try {
const result = await Promise.allSettled(promises);
doSomething(result);
} catch (error) {
showError(error);
}
try {
const result = await Promise.any(promises);
doSomething(result);
} catch (error) {
showError(error);
}
/**
* 6. Private & Public property in Class
* Use of Class Field
*/
class Person {
constructor() {
this._name = "Ajay";
}
}
class Person {
name = "Ajay";
}
// Use of Private Variable/Property
class Person {
#name = "Ajay";
print() {
console.log(this.#name);
}
}
// Old
class Person {
constructor(name) {
this.name = name;
}
}
// Old
class Student extends Person {
constructor(name) {
super(name);
this.study = true
}
}
class Student extends Person {
study = true;
}
/**
* 7. globalThis
* a variable to get the global this irrespective of the environment
* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/globalThis
*/
//on browser
console.log(globalThis);
/**
* 8. Stable Sort
*/
const arr = [
{ name: "Test 1", "total_marks": 80 },
{ name: "Test 2", "total_marks": 70 },
{ name: "Test 3", "total_marks": 90 },
{ name: "Test 32", "total_marks": 90 },
];
arr.sort((a, b) => b.total_marks - a.total_marks);
//Output
[
{ name: "Test 2", "total_marks": 70 },
{ name: "Test 1", "total_marks": 80 },
{ name: "Test 3", "total_marks": 90 },
{ name: "Test 32", "total_marks": 90 },
];
/**
* 9. flat && flatMap()
* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/flatMap
*/
const arr = [1, [2, [3]]];
console.log(arr.flat(Infinity));
// [1,2,[3]]
const arrr1 = [1, 2, 3];
const doubleArr1 = arr1.map(x => [x, x]);
// [[1,1],[2,2],[3,3]]
doubleArr1.flat();
//[1,1,2,2,3,3]
arrr1.flatMap(x => [x, x]);
/**
* 10. Object.fromEntries
*/
const obj = { name: "Ronit", age: 28 };
Object.defineProperties()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment