DEV Community

Discussion on: Awesome 5 javascript Shorthands

Collapse
 
jenbutondevto profile image
Jen

ES2020 and 2021 have brought some more awesome "shortcuts", namely optional chaining which allows you to shorten

let temp = obj.first;
let nestedProp = ((temp === null || temp === undefined) ? undefined : temp.second);
Enter fullscreen mode Exit fullscreen mode

to

let nestedProp = obj.first?.second;
Enter fullscreen mode Exit fullscreen mode

another neat one from 2021 (currently available through babel plugin) is logical assignments, ||=, &&=, ??=.
in the case of &&= you can shorten

let a;
let b;
if(!!b) {
  a = b;
}
// shortens to
a &&= b
Enter fullscreen mode Exit fullscreen mode