r/learnjavascript • u/Confidenceismyname • Feb 12 '21
JavaScript ES2021 - You Need To See These ES12 Features
https://youtu.be/1hFGxkJcTHU25
u/jaredcheeda Feb 12 '21 edited Feb 12 '21
TLDW:
Numeric separators:
// Used for readability
var x = 123_456;
console.log(x); // 123456
console.log(x + 1); // 123457str.replaceAll
var x = 'Cbnbdb';
x = x.replaceAll('b', 'a');
console.log(x); // 'Canada'
// Goodbye x.split('b').join('a'), and goodbye ugly regexWeak Reference
var x = new WeakRef({ example: true });
console.log(x.deref()); // { example: true }
console.log(x.deref().example); // true
// This is used to reduce memory usage by allowing garbage collection sooner, but should be rarely usedPromise.any
function waitAWhile (resolve) {
setTimeout(resolve, Math.round(Math.random() * 10_000));
}
var severalPromises = [
new Promise(waitAWhile),
new Promise(waitAWhile)
];
try {
var firstPromiseToFinish = Promise.any(severalPromises);
console.log(firstPromiseToFinish);
} catch (error) {
console.log(err);
}Logical Assignment Operators
var first = 4;
var second = 8;
// If the first value is anything truthy, reassign it to the second value (which can be anything)
first &&= second;
console.log(first); // 8
var first = null;
var second = 8;
// If the first value is anything falsy, reassign it to the second value (which can be anything)
first ||= second;
console.log(first); // 8
var first = null;
var second = 8;
// If the first value is undefined or null, reassign it to the second value (which can be anything)
first ??= second;
console.log(first); // 814
u/slumdogbi Feb 12 '21
I just need types in vanilla javascript so we can dish typescript for real
13
u/haikusbot Feb 12 '21
I just need types in
Vanilla javascript do we can
Dish typescript for real
- slumdogbi
I detect haikus. And sometimes, successfully. Learn more about me.
Opt out of replies: "haikusbot opt out" | Delete my comment: "haikusbot delete"
8
7
Feb 12 '21
So we're never gonna not need Babel
0
Feb 12 '21
[deleted]
-4
u/superluminary Feb 12 '21 edited Feb 13 '21
Um, typescript is literally compiled to ES5 using Babel.
EDIT: all you people downvoting me better not be using CRA, because you’re literally transpiling TypeScript with Babel every time you press save.
0
Feb 12 '21
[deleted]
5
-5
u/superluminary Feb 12 '21
Sorry, when I said typescript is compiled using Babel I perhaps sounded a little flippant. I didn’t mean to offend.
I am still correct though and I think you may have misunderstood what I meant.
You are correct that the TSC compiler does not bundle Babel. However, I am perfectly correct when I say that typescript can and is compiled to ES5 using Babel. Any CRA TS project will feature a two stage compile, a fast Babel driven compilation, followed by a slower TSC type check.
Babel will not check types, but it will compile Typescript very adequately, and is the preferred choice in most projects I’ve worked on because it’s so much faster.
TSC also exists, but because it’s so much slower, it’s typically used as a second stage type checking tool.
0
Feb 13 '21
[deleted]
-5
u/superluminary Feb 13 '21
Except it’s not semantics. Typescript doesn’t work just fine in a browser unless you transpile it, and for most people writing plain old React, transpilation will be via Babel. You may be using TSC. That’s cool too.
0
Feb 13 '21
[deleted]
-1
u/superluminary Feb 13 '21
So we replace Babel transpilation with TSX transpilation. Either way, we still need a transpiler. The point remains, we need a transpiler and will continue to need one for the foreseeable future.
Babel accepts and transpiles Typescript. So does TSC, but you can’t use Typescript in a browser without a transpilation step.
15
u/davidmdm Feb 12 '21
I wish they would stop. There’s already plenty of stuff in JS. It’s just making JS have a higher cognitive load.
8
u/Gman_711 Feb 12 '21
It's what I hate about Java.. you can do almost nothing without references because the api surface is immense.
4
u/oze4 Feb 12 '21
It's why I love Golang, well one of the many reasons lol...(for backend, obviously)
5
u/davidmdm Feb 12 '21
Here here. I too am a gopher. But before I was a gopher I was a nodejs developer. Go really spoils us with its lack of features and I mean that seriously
1
u/oze4 Feb 12 '21
No doubt. Go is like a Picasso painting. Simple, weird. It works so well but at times I'm not sure what I'm looking at.
Whereas Node is like one of those paintings from those videos where people hang a paint can from a rope, poke holes in the bottom of it, then spin it in circles above the canvas.
2
1
u/simkessy Feb 13 '21
You don't have to use them. I've never seen new Set or new Map on prod yet. {} Works just fine
1
u/davidmdm Feb 13 '21
It’s not about how I choose to write. Writing code is easy. What’s hard is to read code. I feel like a lot of these new assignment operators are just going to make code more clever and harder to read.
4
u/USKillbotics Feb 12 '21
I appreciate the overview, but it seems like we (JS) are running out of ideas here.
1
1
1
u/morningcoma Feb 12 '21
While I do recognize all these as QoL features I also believe that some of these features just lead us even further down the path of "wat?".
0
Feb 12 '21
i only cared about ES6 and those = () => {}
and this es2021 again gives me headache. sometimes i see why older devs stick with legacy codebase like vanilla php or older js
1
34
u/superluminary Feb 12 '21 edited Feb 12 '21
Can we just have a list? I don’t have 13 minutes to watch a video.
Edit: actually, that was a good video and I enjoyed it.