Posted on

ES2023 (ECMAScript 2023) Features

ES2023 focused on minor improvements and consistency updates.

1. Array.prototype.toSorted(), toSpliced(), and toReversed()

  • Immutable versions of sort(), splice(), and reverse(), preventing in-place modifications.

Example:

const nums = [3, 1, 4];

console.log(nums.toSorted()); // ✅ [1, 3, 4] (original array remains unchanged)
console.log(nums.toReversed()); // ✅ [4, 1, 3]
console.log(nums.toSpliced(1, 1, 99)); // ✅ [3, 99, 4] (removes index 1, adds 99)

console.log(nums); // ✅ [3, 1, 4] (unchanged)

2. Array.prototype.findLast() and findLastIndex()

  • Similar to find() and findIndex(), but search from the end.

Example:

const arr = [1, 2, 3, 4, 5];

console.log(arr.findLast(n => n % 2 === 0)); // ✅ 4
console.log(arr.findLastIndex(n => n % 2 === 0)); // ✅ 3

3. RegExp.prototype.hasIndices

  • Checks if a regex was created with the /d flag.

Example:

const regex = /test/d;
console.log(regex.hasIndices); // ✅ true

4. Symbol.prototype.description Now Writable

  • The description property of Symbol objects can be modified.

Example:

const sym = Symbol("original");
console.log(sym.description); // ✅ "original"

5. WeakMap.prototype.emplace() and WeakSet.prototype.emplace() (Proposal)

  • A shortcut for setting values only if a key doesn’t already exist. (Not finalized but expected in future updates.)

Example:

const weakMap = new WeakMap();
weakMap.emplace({}, () => "newValue"); // ✅ Sets value only if key doesn’t exist

Summary of Features

FeatureES2022ES2023
Private fields/methods in classes
Static fields/methods in classes
Object.hasOwn()
RegExp /d flag (match indices)
Error.cause
Array.prototype.at()
Top-level await in modules
Array.prototype.toSorted(), toReversed(), toSpliced()
Array.prototype.findLast() and findLastIndex()
RegExp.prototype.hasIndices
Symbol.prototype.description writable