Last active
January 30, 2020 09:40
-
-
Save codehag/a829bbd39be9c9879ed7a9076363bb1c to your computer and use it in GitHub Desktop.
changes in js 2020
This file contains hidden or 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
var regex = /t(e)(st(\d?))/g; | |
var string = 'test1test2'; | |
string.match(regex); // gives ['test1', 'test2'] - how do i get the capturing groups? | |
var matches = []; | |
var lastIndexes = {}; | |
var match; | |
lastIndexes[regex.lastIndex] = true; | |
while (match = regex.exec(string)) { | |
lastIndexes[regex.lastIndex] = true; | |
matches.push(match); | |
// example: ['test1', 'e', 'st1', '1'] with properties `index` and `input` | |
} | |
matches; | |
matchAll(string, regex) | |
var s = "ECMAScript in 2020"; | |
for (var w of s.matchAll(/\w+/g)) { | |
print(w) // => "2020" | |
} | |
var r = /\w+/g; | |
var w; | |
while ((w = r.exec(s)) !==null) { | |
print(w) // => "2020" | |
} | |
import defaultExport from "module-name"; | |
const main = document.querySelector("main"); | |
for (const link of document.querySelectorAll("nav > a")) { | |
link.addEventListener("click", e => { | |
// etc. | |
import(`./${link}.js`) | |
.then(module => { | |
module.loadPageInto(main); | |
}) | |
.catch(err => { | |
// etc. | |
}); | |
}); | |
} | |
function reflect(promise) { | |
return promise.then( | |
(v) => ({ status: 'fulfilled', value: v }), | |
(error) => ({ status: 'rejected', reason: error }) | |
); | |
} | |
const promises = [ fetch('index.html'), fetch('https://does-not-exist/') ]; | |
const results = await Promise.all(promises.map(reflect)); | |
const successfulPromises = results.filter(p => p.status === 'fulfilled'); | |
const promises = [ fetch('index.html'), fetch('https://does-not-exist/') ]; | |
const results = await Promise.allSettled(promises); | |
const successfulPromises = results.filter(p => p.status === 'fulfilled'); | |
2 ** 64 | |
// => 18446744073709552000 | |
2n ** 64n | |
// => 18446744073709551616n | |
const x = Number.MAX_SAFE_INTEGER; | |
// ↪ 9007199254740991, this is 1 less than 2^53 | |
const y = x + 1; | |
// ↪ 9007199254740992, ok, checks out | |
const z = x + 2 | |
// ↪ 9007199254740992, wait, that’s the same as above! | |
const theBiggestInt = 9007199254740991n; | |
const alsoHuge = BigInt(9007199254740991); | |
// ↪ 9007199254740991n | |
const biggerThanThat = theBiggestInt + 2n; | |
// ↪ 9007199254740993n | |
// optional chaining | |
var street = user.address ? user.address.street : undefined; | |
var result = user.method ? user.method() : undefined; | |
var dynamic = user.address ? user.address[field] : undefined; | |
var street = user.address?.street | |
var result = user.method?.(); | |
var dynamic = user.address?.[field]; | |
//nullish coalescing | |
const undefinedValue = undefined || 'default'; // 'default' | |
const nullValue = null || 'default'; // 'default' | |
const headerText = '' || 'Hello, world!'; // 'Hello, world!' | |
const animationDuration = 0 || 300; // 300 | |
const showSplashScreen = false || true; // true | |
const undefinedValue = undefined ?? 'default'; // 'default' | |
const nullValue = null ?? 'default'; // 'default' | |
const headerText = '' ?? 'Hello, world!'; // '' | |
const animationDuration = 0 ?? 300; // 0 | |
const showSplashScreen = false ?? true; // false | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment