ES2023 Features!

Array find from last

Array.prototype.findLast and Array.prototype.findLastIndex ๐Ÿ“•

let nums = [5,4,3,2,1];

let lastEven = nums.findLast((num) => num % 2 === 0); // 2

let lastEvenIndex = nums.findLastIndex((num) => num % 2 === 0); // 3

Hashbang Grammar

#! for JS ๐Ÿ“•

The first line of this script begins with #!, which indicates a comment that can contain any text.

#!/usr/bin/env node
// in the Script Goal
'use strict';
console.log(1);

Symbols as WeakMap keys

Use symbols in weak collections and registries ๐Ÿ“•

Note: registered symbols arenโ€™t allowed as weakmap keys.

let sym = Symbol("foo");
let obj = {name: "bar"};
let wm = new WeakMap();
wm.set(sym, obj);
console.log(wm.get(sym)); // {name: "bar"}
sym = Symbol("foo");
let ws = new WeakSet();
ws.add(sym);
console.log(ws.has(sym)); // true
sym = Symbol("foo");
let wr = new WeakRef(sym);
console.log(wr.deref()); // Symbol(foo)
sym = Symbol("foo");
let cb = (value) => {
  console.log("Finalized:", value);
};
let fr = new FinalizationRegistry(cb);
obj = {name: "bar"};
fr.register(obj, "bar", sym);
fr.unregister(sym);

Change Array by copy

return changed Array and TypedArray copies ๐Ÿ“•

Note: typed array doesnโ€™t have tospliced.

const greek = ['gamma', 'alpha', 'beta']
greek.toSorted(); // [ 'alpha', 'beta', 'gamma' ]
greek; // [ 'gamma', 'alpha', 'beta' ]

const nums = [0, -1, 3, 2, 4]
nums.toSorted((n1, n2) => n1 - n2); // [-1,0,2,3,4]
nums; // [0, -1, 3, 2, 4]
const greek = ['gamma', 'alpha', 'beta']
greek.toReversed(); // [ 'beta', 'alpha', 'gamma' ]
greek; // [ 'gamma', 'alpha', 'beta' ]
const greek = ['gamma', 'alpha', 'beta']
greek.toSpliced(1,2); // [ 'gamma' ]
greek; // [ 'gamma', 'alpha', 'beta' ]

greek.toSpliced(1,2, ...['delta']); // [ 'gamma', 'delta' ]
greek; // [ 'gamma', 'alpha', 'beta' ]
const greek = ['gamma', 'alpha', 'beta']
greek.toSpliced(1,2); // [ 'gamma' ]
greek; // [ 'gamma', 'alpha', 'beta' ]

greek.toSpliced(1,2, ...['delta']); // [ 'gamma', 'delta' ]
greek; // [ 'gamma', 'alpha', 'beta' ]
const greek = ['gamma', 'alpha', 'beta'];
greek.with(2, 'bravo'); // [ 'gamma', 'alpha', 'bravo' ]
greek; //  ['gamma', 'alpha', 'beta'];

With โค๏ธ Hemanth.HM