New in JavaScript ('18-'19)

Numeric Separators

let salary = 1000000000000;

let salary = 1_000_000_000_000;

- Shipping in Chrome 75

- Transpilers like Babel support it

flat()

const array = [1, [2, [3]]];

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

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

array.flat(Infinity);    // [1, 2, 3]

Map and flatten

let text = ["I do love", "JavaScript."];

let mapped = text.map(currentElement => currentElement.split(" "));

let flattened = mapped.flat();

// [["I", "do", "love"], ["JavaScript."]]

// ["I", "do", "love", "JavaScript."]

let mappedAndFlattened = text.map(currentElement => currentElement.split(" ")).flat();

// ["I", "do", "love", "JavaScript."]

🐌

flatMap()

let text = ["I do love", "JavaScript."];

let mappedAndFlattened = text.flatMap(currentElement => currentElement.split(" "));

// ["I", "do", "love", "JavaScript."]

🚀

Access global "this" value

Browser, Node.js, JavaScript Engine Shell

const getGlobalThis = () => {
  if (typeof self !== "undefined") return self;
  if (typeof window !== "undefined") return window;
  if (typeof global !== "undefined") return global;
  if (typeof this !== "undefined") return this;
  throw new Error("Unable to locate global object");
};

- Browsers: window

- Web/Service Workers: window is not available, so check for self

- Node: global

- Stand-alone JavaScript Shells: those don't work, so fall back to this

Outside of Browsers within Modules: don't run in the global scope

Strict mode functions: this is undefined

Bundlers wrap source code: this might refer to something else

const theGlobalThis = globalThis;

Sort

const students = [
  {name: "Alex", mark: 7},
  {name: "Anda", mark: 10},
  {name: "Gabi", mark: 9},
  {name: "Ioan", mark: 8},
  {name: "Vlad", mark: 10},
];

students.sort((a, b) =>

b.mark  a.mark);

const students = [
  {name: "Anda", mark: 10},
  {name: "Vlad", mark: 10},
  {name: "Gabi", mark: 9},
  {name: "Ioan", mark: 8},
  {name: "Alex", mark: 7},
];
const students = [
  {name: "Vlad", mark: 10},
  {name: "Anda", mark: 10},
  {name: "Gabi", mark: 9},
  {name: "Ioan", mark: 8},
  {name: "Alex", mark: 7},
];

Some JavaScript engines wold use:

- stable sort for short arrays

- unstable sort for large arrays

Array sort is now stable

Promise

Two new proposals are making their way through the standardisation process

Promise.all all promises have fulfilled or one of them rejects
(short-circuits when an input value is rejected)
Promise.race as soon as one of the promises fulfilled of rejects
(short-circuits when an input value is settled)
Promise.allSettled

 
Promise.any

 

76

Nightly

Polyfill

(Early stages of standardisation)

only after all the original promises have settled, i.e. become either fulfilled or rejected
(i.e. all work is done) (does not short-circuit)

as soon as one of the promises fulfilled; does not reject early when one of the promises rejects
(short-circuits when an input value is fulfilled)

Thanks!

What’s new in JavaScript (Google I/O ’19)

https://www.youtube.com/watch?v=c0oy0vQKEZE

New in JavaScript ('18-'19)

By vladmilinovici

New in JavaScript ('18-'19)

  • 451