What's new

What`s new?!

Ecma script

Last updated: March 15, 2024 at 12:00 AM

Ecma script features

from revolutionary es6 till current latest release

Ecma script main features on each release are listed below, click on each version title to see the features.

ES2024

Symbols as WeakMap keys

Symbols can now be used as keys in WeakMaps

const sym = Symbol("key");
const weakMap = new WeakMap();
weakMap.set(sym, "value");

Array.fromAsync

Create an array from async iterables

const asyncGen = async function*() { yield 1; yield 2; };
console.log(await Array.fromAsync(asyncGen()));

ES2023

Array.prototype.toSorted

Sort without mutating original array

const arr = [3, 1, 2];
console.log(arr.toSorted());

Hashbang (#!) in JavaScript files

Support for Unix-style shebang

#!/usr/bin/env node
console.log("Hello, Node.js!");

ES2022

Top-Level Await

Use await outside of async functions

const data = await fetch("https://api.example.com").then(res => res.json());

Object.hasOwn

Check for own properties safely

console.log(Object.hasOwn({a: 1}, "a"));

ES2021

String.prototype.replaceAll

Replace all occurrences of a substring

console.log("hello hello".replaceAll("hello", "hi"));

Numeric Separators

Improve number readability

const num = 1_000_000;

ES2020

Nullish Coalescing Operator (??)

Return right operand if left is null or undefined

console.log(null ?? "default"); // "default"

Optional Chaining (?.)

Avoid errors when accessing deep properties

const obj = {};
console.log(obj?.prop?.nested);

ES2019

Array.prototype.flat

Flatten nested arrays

console.log([1, [2, [3]]].flat(2));

String.prototype.trimStart & trimEnd

Remove whitespace from start or end of a string

console.log("  Hello  ".trimStart());
console.log("  Hello  ".trimEnd());

Object.fromEntries

Transform key-value pairs into objects

console.log(Object.fromEntries([["a", 1], ["b", 2]]));

Optional Catch Binding

Omit catch parameter if unused

try {
throw new Error("Oops");
} catch {
console.log("Error caught");
}

ES2018

Rest/Spread Properties

Spread objects

const obj = {a: 1, b: 2};
const clone = {...obj};

Promise.prototype.finally

Execute code after promise resolution/rejection

fetchData().finally(() => console.log("Done"));

Asynchronous Iteration

Use for await...of for async iterables

async function process(items) {
for await (const item of items) {
console.log(item);
}
}

RegExp Enhancements

Named capture groups, lookbehind assertions

const regex = /(?<year>\d{4})-(?<month>\d{2})/;
const match = regex.exec("2023-04");
console.log(match.groups.year); // "2023"

ES2017

Async/Await

Syntactic sugar for promises

async function fetchData() {
return await Promise.resolve("Data");
}

Object.values and Object.entries

Get object values and key-value pairs

console.log(Object.values({a: 1, b: 2}));
console.log(Object.entries({a: 1, b: 2}));

String.prototype.padStart & padEnd

Pad strings with characters

console.log("5".padStart(3, "0")); // "005"
console.log("5".padEnd(3, "0")); // "500"

Trailing commas in function parameters

Allow trailing commas in function definitions

function foo(a, b,) {
console.log(a, b);
}

Shared memory and Atomics

Enable multi-threaded operations

const sharedBuffer = new SharedArrayBuffer(16);
const int32 = new Int32Array(sharedBuffer);
Atomics.store(int32, 0, 123);

ES2016

Array.prototype.includes

Check if an array contains an element

console.log([1, 2, 3].includes(2)); // true

Exponentiation Operator

** as a shorthand for Math.pow

console.log(2 ** 3); // 8

ES2015 (es6)

let and const

Block-scoped variable declarations

let x = 10;
const y = 20;

Arrow Functions

Shorter function syntax with lexical this

const add = (a, b) => a + b;

Template Literals

String interpolation using backticks

const name = "Jafar";
console.log(`Hello, ${name}!`);