| title | Ecma script | |||
|---|---|---|---|---|
| image | /images/technologies/Ecmascript.png | |||
| updated | 2024-03-15 | |||
| description | Ecmascript is a standardized version of JavaScript. | |||
| tags |
|
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.
Symbols can now be used as keys in WeakMaps
const sym = Symbol("key");
const weakMap = new WeakMap();
weakMap.set(sym, "value");Create an array from async iterables
const asyncGen = async function*() { yield 1; yield 2; };
console.log(await Array.fromAsync(asyncGen()));Sort without mutating original array
const arr = [3, 1, 2];
console.log(arr.toSorted());Support for Unix-style shebang
#!/usr/bin/env node
console.log("Hello, Node.js!");Use await outside of async functions
const data = await fetch("https://api.example.com").then(res => res.json());Check for own properties safely
console.log(Object.hasOwn({a: 1}, "a"));Replace all occurrences of a substring
console.log("hello hello".replaceAll("hello", "hi"));Improve number readability
const num = 1_000_000;Return right operand if left is null or undefined
console.log(null ?? "default"); // "default"Avoid errors when accessing deep properties
const obj = {};
console.log(obj?.prop?.nested);Flatten nested arrays
console.log([1, [2, [3]]].flat(2));Remove whitespace from start or end of a string
console.log(" Hello ".trimStart());
console.log(" Hello ".trimEnd());Transform key-value pairs into objects
console.log(Object.fromEntries([["a", 1], ["b", 2]]));Omit catch parameter if unused
try {
throw new Error("Oops");
} catch {
console.log("Error caught");
}Spread objects
const obj = {a: 1, b: 2};
const clone = {...obj};Execute code after promise resolution/rejection
fetchData().finally(() => console.log("Done"));Use for await...of for async iterables
async function process(items) {
for await (const item of items) {
console.log(item);
}
}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"Syntactic sugar for promises
async function fetchData() {
return await Promise.resolve("Data");
}Get object values and key-value pairs
console.log(Object.values({a: 1, b: 2}));
console.log(Object.entries({a: 1, b: 2}));Pad strings with characters
console.log("5".padStart(3, "0")); // "005"
console.log("5".padEnd(3, "0")); // "500"Allow trailing commas in function definitions
function foo(a, b,) {
console.log(a, b);
}Enable multi-threaded operations
const sharedBuffer = new SharedArrayBuffer(16);
const int32 = new Int32Array(sharedBuffer);
Atomics.store(int32, 0, 123);Check if an array contains an element
console.log([1, 2, 3].includes(2)); // true** as a shorthand for Math.pow
console.log(2 ** 3); // 8