New Upcoming JavaScript features in 2020

New Upcoming JavaScript features in 2020

You should know if you use JavaScript everyday. That is "New Upcoming JavaScript features" in this post

Upcoming new JavaScript features You should know if you use JavaScript everyday

Since ECMAScript2015 (also called ES6) was released, JavaScript has changed and improved widely. This is excellent news for all of the JavaScript developers. Furthermore, a new ECMAScript version has released every year. You likely didn’t notice what features were added in the latest ECMAScript, which was released in June 2019. I will briefly show you the new features added in the latest version and talk about the new features for the future version.

The features that I will show you are NOT yet decided to be in the next version. All of what I will talk about in this post are currently in stage 3. Check out this repo if you want to get more details.

Features in ECMAScript2019 (ES10)

1. Array.prototype.flat

A method that creates a new array with all sub-array elements concatenated into it recursively up to the specified depth.

const array = [1, 2, [3, 4]];
array.flat(); // [1, 2, 3, 4];

This is very useful, especially when you want to flatten your nested array. If the depth of your array is deeper than one depth, calling flat once can’t entirely flatten your array. flat takes a parameter for depth, which refers to how many depths you want it to go into to flatten the array.

// Crazy example
const crazyArray = [1, 2, [3, 4], [[5], [6, [7,8]]]];
crazyArray.flat(Infinity); // [1, 2, 3, 4, 5, 6, 7, 8];
// The parameter must be the number type

The deeper you want to search the array, the more computing time will be required to flatten it. Note that IEs and Edge do not support this feature.

2. Array.prototype.flatMap

A method first maps each element using a mapping function, then flattens the result into a new array.

const arr = ["it's Sunny in", "", "California"];
arr.flatMap(x => x.split(" "));
// ["it's","Sunny","in", "", "California"]

The difference between flat and flatMap is that you can put a custom function in flatMap to manipulate each value. Additionally, unlike flat, flatMap flattens one depth array, only. The return value should be an array type. This would be very useful when you should do something before flattening the array.

There were more features added to ES10. if you want to know more about them.

New Features In Stage 3

In stage 3, there are a few interesting features suggested. I will introduce some of them to you briefly.

Numeric Separators

When you assigned a big number to a variable, weren’t you confused on how big that number is or if you wrote it right? This proposal allows you to put an underscore between numbers so you can count it easier.

1_000_000_000           // Ah, so a billion
101_475_938.38          // And this is hundreds of millions

let fee = 123_00;       // $123 (12300 cents, apparently)
let fee = 12_300;       // $12,300 (woah, that fee!)
let amount = 12345_00;  // 12,345 (1234500 cents, apparently)
let amount = 123_4500;  // 123.45 (4-fixed financial)
let amount = 1_234_500; // 1,234,500

let budget = 1_000_000_000_000;
// What is the value of `budget`? It's 1 trillion!
// 
// Let's confirm:
console.log(budget === 10 ** 12); // true

It will be up to each developer whether to use this feature once it’s released, but one thing’s for sure, this feature would reduce your headaches for counting how big a number is!

Top-level await

Top-level _await_ enables modules to act as big async functions: With top-level _await_, ECMAScript Modules (ESM) can _await_ resources, causing other modules who _import_ them to wait before they start evaluating their body.

The motivation of this feature was that when you import a module which has async function, the output of the async function is undefined.

// awaiting.mjs
import { process } from "./some-module.mjs";
const dynamic = import(computedModuleSpecifier);
const data = fetch(url);
export const output = process((await dynamic).default, await data);

There are two files. output could be undefined if it’s called before the Promises tasks are done.

// usage.mjs
import { output } from "./awaiting.mjs";
export function outputPlusValue(value) { return output + value }
console.log(outputPlusValue(100));
setTimeout(() => console.log(outputPlusValue(100), 1000);

usage.mjs will not execute any of the statements in it until the awaits in awaiting.mjs have had their Promises resolved.

Nullish Coalescing for JavaScript

This would be one of the most useful features amongst proposals in stage 3. We often wrote this kind of code.

const obj = { 
  name: 'James'
};

const name = obj.name || 'Jane'; // James

If obj.name is falsy, then return ‘Jane’, so undefined won’t be returned. But the problem is, an empty string(‘’) is also considered falsy in this case. Then we should rewrite it again like this below.

const name = (obj.name && obj.name !== '') || 'Jane';

It is a pain in the neck to write the code like that every time. This proposal allows you to check null and undefined only.

const response = {
  settings: {
    nullValue: null,
    height: 400,
    animationDuration: 0,
    headerText: '',
    showSplashScreen: false
  }
};

const undefinedValue = response.settings.undefinedValue ?? 'some other default'; // result: 'some other default'
const nullValue = response.settings.nullValue ?? 'some other default'; // result: 'some other default'
const headerText = response.settings.headerText ?? 'Hello, world!'; // result: ''
const animationDuration = response.settings.animationDuration ?? 300; // result: 0
const showSplashScreen = response.settings.showSplashScreen ?? true; // result: false

Optional Chaining

This proposal goes with Nullish Coalescing for JavaScript, especially in TypeScript. TypeScript has announced that they will include Nullish Coalescing for JavaScript and this proposal in their next released version, 3.7.0.

const city = country && country.city; 
// undefined if city doesn't exist

Look at the example code. To get city, which is in country object, we should check if country exists and if city exists in country.

With Optional Chaining, this code can be refactored like this.

const city = country?.city; // undefined if city doesn't exist

This feature seems very handy and useful for this situation.

import { fetch } from '../yourFetch.js';
(async () => {
  const res = await fetch();
  // res && res.data && res.data.cities || undefined
  const cities = res?.data?.cities;
})();

Promise.any

_Promise.any_ accepts an iterable of promises and returns a promise that is fulfilled by the first given promise to be fulfilled, or rejected with an array of rejection reasons if all of the given promises are rejected.

With async-await,

try {
  const first = await Promise.any(promises);
  // Any of the promises was fulfilled.
} catch (error) {
  // All of the promises were rejected.
}

With Promise pattern,

Promise.any(promises).then(
  (first) => {
    // Any of the promises was fulfilled.
  },
  (error) => {
    // All of the promises were rejected.
  }
);

Since there were Promise all, allSettled, and race, there wasn’t any. So this feature is simple but powerful for a needed situation.

However, this proposal wasn’t tested yet, so this proposal might have a longer time to be accepted in a future version of ECMAScript.

Conclusion

There are so many interesting proposals in stage 3. I can’t wait to meet them in ES11 or ES12. Of course, I won’t need all of them, but some of them would definitely make my codes more elegant.

Thank you ! If you liked this post, share it with all of your programming buddies!

JavaScript Nodejs Coding Web Development Programming

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Hire Nodejs Developer

Looking to **hire NodeJS Developer**[https://www.dataeximit.com/node-js-development/](https://www.dataeximit.com/node-js-development/ "https://www.dataeximit.com/node-js-development/") to build a perfect back-end to your website or...

Why Web Development is Important for your Business

With the rapid development in technology, the old ways to do business have changed completely. A lot more advanced and developed ways are ...

Hire Dedicated eCommerce Web Developers | Top eCommerce Web Designers

Build your eCommerce project by hiring our expert eCommerce Website developers. Our Dedicated Web Designers develop powerful & robust website in a short span of time.

Web Development 2020 vs 2010

Things have changed pretty fast from what was a decade ago for web development and the facts are totally mind-blowing. I mean a web developer now makes mobile apps, desktop app, backend API, games and so much more.

How long does it take to develop/build an app?

This article covers A-Z about the mobile and web app development process and answers your question on how long does it take to develop/build an app.