Sitemap

ECMAScript 2023 (ES14): Four New Features

2 min readJun 26, 2023

--

Press enter or click to view image in full size

Introduction

ECMAScript 2023, due in June, is set to add new methods for searching and changing arrays, allow symbols to be used as WeakMap keys, and standardize support for hashbang grammar.

Change Array by Copy

toReversed

The toReversed method is used to create a new array that is the reverse of the original array. The original array remains unchanged.

toReversed and reverse methods both are used to reverse an array, toReversed creates a new array while reverse modifies the original array.

const numbers = [1, 2, 3, 4, 5];
const reversedElement = numbers.toReversed();

console.log(numbers); //[1, 2, 3, 4, 5]
console.log(reversedElement); //[5, 4, 3, 2, 1]

toSorted

The toSorted method is used to sort the elements of an array in a particular order.It doesn’t change original array.

toSorted and sort methods both are used to sort an array, toSorted creates a new array while sort modifies the original array.

const numbers = [3, 4, 1, 5, 2];
const sortedNumbers = numbers.toSorted();

console.log(numbers); // Output: [3, 4, 1, 5, 2]
console.log(sortedNumbers); // Output: [1, 2, 3, 4, 5]

toSpliced

Get Chandramathi Muthuraj’s stories in your inbox

Join Medium for free to get updates from this writer.

The toSpliced method is used to remove elements from an array. It returns a new array with the specified elements removed, leaving the original array unchanged.

toSpliced method returns the updated array but the splice method returns only spliced elements. toSpliced creates a new array while splice modifies the original array.

Example for toSpliced

const numbers = [1, 2, 3, 4, 5];
const usingtoSpliced = numbers.toSpliced(1,2,"Chandra")

console.log('numbers', numbers) //Output: [1, 2, 3, 4, 5]
console.log('usingtoSpliced', usingtoSpliced) //Output: [1, 'Chandra', 4, 5]
console.log('numbers', numbers) //Output: [1, 2, 3, 4, 5]

Example for Splice

const numbers = [1, 2, 3, 4, 5];
const usingSplice = numbers.splice(1,2,"Chandra")

console.log('numbers', numbers) //Output: [1, 'Chandra', 4, 5]
console.log('usingSplice', usingSplice) //Output: [2, 3]
console.log('numbers', numbers) //Output: [1, 'Chandra', 4, 5]

with(index, value)

The with() method returns a copy of the array with the value at the specified index replaced.

const number = [1, 2, 3, 4];
const replaced = number.with(2, 99);
console.log(number); // Output: [ 1, 2, 3, 4 ]
console.log(replaced); // Output: [ 1, 2, 99, 4 ]

Can I use them today?

Currently, the methods toReversed, toSorted, and toSpliced are not included in the official ECMAScript standard. However, they are in Stage 3 of the specification process, indicating that they are nearing completion and might be included in future versions of JavaScript.

However, Chrome has these methods available since version 110.

What’s Next?

  • Finding Elements from last
  • Symbols as WeakMap Keys
  • Hashbang Grammar

Follow me on LinkedIn!!!

--

--

Chandramathi Muthuraj
Chandramathi Muthuraj

Written by Chandramathi Muthuraj

The only thing between you and success is you