ECMAScript 2023 is slated to bring new techniques for exploring and modifying arrays, allow the usage of symbols as WeakMap keys, and standardise support for hashbang syntax.
Change Array By Copy
Returning a new copy of the array with the change rather than updating the original array, adds more methods on Array.prototype for changing the array.
New Array.prototype introduced functions are:
- Array.prototype.toSorted(compareFn)
- Array.prototype.toReversed()
- Array.prototype.toSpliced(start, deleteCount, …items)
- Array.prototype.with(index, value)
const num = [1, 2, 3, 4, 5, 6, 7, 8, 9] /* toSorted */ const sortedArr = num.toSorted(); console.log("sorted", sortedArr); // "sorted", [1, 2, 3, 4, 5, 6, 7, 8, 9] console.log("original", num); // "original", [1, 2, 3, 4, 5, 6, 7, 8, 9] /* toReversed */ const reversed = num.toReversed(); console.log("original", num); // "original", [1, 2, 3, 4, 5, 6, 7, 8, 9] console.log("reversed", reversed); // "reversed", [9, 8, 7, 6, 5, 4, 3, 2, 1] /* with */ const replaceWith = num.with(1, 150); console.log("with", replaceWith); // "with", [1, 150, 3, 4, 5, 6, 7, 8, 9] console.log("original", num); // "original", [1, 2, 3, 4, 5, 6, 7, 8, 9] /* toSpliced */ const splicedArr = num.toSpliced(0, 4); console.log("toSpliced", splicedArr); // "toSpliced", [5, 6, 7, 8, 9] console.log("original", num); // "original", [1, 2, 3, 4, 5, 6, 7, 8, 9]
Find Array From The Last
With the help of this function, we can use a condition to find an element in the array from last to first.
const array = [{a: 1, b: 1}, {a: 2, b: 2}, {a: 3, b: 3}, {a: 4, b: 4}] console.log(array.findLastIndex(n => n.a * 5 === 21)); // output-> -1 as the condition is not justified for returning the last element. console.log(array.findLastIndex(n => n.a * 5 === 20)); // output-> 3 which is the index of the last element as the condition is true. console.log(array.findLast(n => n)); //output -> {a: 4,b: 4 } console.log(array.findLast(n => n.a * 5 === 20)); // output-> {a:4,b:4} as the condition is true so it returns the last element. console.log(array.findLast(n => n.a * 5 === 21)); //output-> undefined as the condition is false so return undefined instead of {a:4,b:4}.
Hashbang Grammer
We would be able to use Hashbang and Shebang in certain CLI thanks to this functionality.
Shebang, denoted by #! is a special line at the start of the script that instructs the operating system to use a specific interpreter when running the script.
#!/usr/bin/env node // in the Script Goal 'use strict'; console.log(3*3); #!/usr/bin/env node // in the Module Goal export {}; console.log(4*4);
/usr/bin/env node #! This line would run the Node.js source code as an independent executable.
If we were to use the node interpreter, such as node./file, to specifically launch a file, we wouldn’t require this line (#!/usr/bin/env node).
Symbols As WeakMap Keys
This enables the use of special Symbols as keys. WeakMaps can only use objects as keys at the moment. WeakMaps employ objects as keys since they have a common identification characteristic.
Use Symbol as the key rather than generating a new object with WeakMap because it is the only primitive type in ECMAScript that supports unique values.
const weak = new WeakMap(); const key = Symbol('my ref'); const object = { a:1 }; weak.set(key, object); console.log(weak.get(key));