DEV Community

Code Craft-Fun with Javascript
Code Craft-Fun with Javascript

Posted on • Updated on

New Features in ES2023

New Methods in Array.prototype - Copy Array

The reverse(), sort() and splice() methods on Array.prototype mutate the array in place. To avoid the mutation es2023 introduced the equivalent methods that returns the copy of the array on which these operations are performed.

toReversed() method

const original = [1, 2, 3, 4];
const reversed = original.toReversed();

console.log(original);
// [ 1, 2, 3, 4 ]

console.log(reversed);
// [ 4, 3, 2, 1 ]
Enter fullscreen mode Exit fullscreen mode

toSorted(compareFn) method

const original = [1, 3, 2, 4];
const sorted = original.toSorted();

console.log(original);
// [ 1, 3, 2, 4 ]

console.log(sorted);
// [ 1, 2, 3, 4 ]
Enter fullscreen mode Exit fullscreen mode

toSpliced(start, deleteCount, ...items) method

const original = [1, 4];
const spliced = original.toSpliced(1, 2);

console.log(original);
// [ 1, 4 ]

console.log(spliced);
// [ 1, 2, 3, 4 ]
Enter fullscreen mode Exit fullscreen mode

with(index, value) method

const original = [1, 2, 3, 4];
const withThree = original.with(2, 5);

console.log(original);
// [ 1, 2, 3, 4 ]

console.log(withThree);
// [ 1, 2, 5, 4 ]
Enter fullscreen mode Exit fullscreen mode

Use of Symbols as WeakMap Keys

In JavaScript, Objects and Symbols are guaranteed to be unique and cannot be re-created, which makes them both great candidates for the WeakMap keys. Previous versions or specifications allowed only Objects to be used that way, but in es2023 we can use Symbols as the WeakMap key.

const weak = new WeakMap();
const key = Symbol("ref");
weak.set(key, "ECMAScript 2023");

console.log(weak.get(key));
// ECMAScript 2023
Enter fullscreen mode Exit fullscreen mode

Array find from last

es2023 adds findLast() and findLastIndex() methods on Array and TypedArray prototype. They are similar to find() and findIndex() but iterates over items in reverse order.

const isEven = (number) => number % 2 === 0;
const numbers = [1, 2, 3, 4];

// from first to the last lookup
console.log(numbers.find(isEven));
// 2
console.log(numbers.findIndex(isEven));
// 1

// from last to the first lookup
console.log(numbers.findLast(isEven));
// 4
console.log(numbers.findLastIndex(isEven));
// 3
Enter fullscreen mode Exit fullscreen mode

Hashbang Grammar

Hashbang, also known as a shebang is a sequence of characters at the beginning of an executable script that defines the interpreter for the program to be run on.

#!/usr/bin/env node

console.log('hello');
Enter fullscreen mode Exit fullscreen mode

With this change we would not require to invoke a file explicitly using node interpreter as node <file-path>

These new features will going to be very handy in lots of use cases. Hope you find it useful and are eagerly waiting to use these features in your upcoming projects.😀

Top comments (1)

Collapse
 
codecraftjs profile image
Code Craft-Fun with Javascript

What are your thoughts on these new features?