New features in ES2023 JavaScript
ES2023 is the version of ECMAScript released in this year. Checkout features that have been incorporated and could be used in our Javascript projects.
Features of ES2023
-
- Find array from the last
- Hashbang Grammer
- Symbol as WeakMap keys
- Change array by copy
Find array from the last
The findLast() method will allow us to find element from the last to first of the array based on a condition.
1 2 3 4 5 6 7 |
const ages = [3, 10, 18, 20]; function checkAge(age) { return age > 18; } const results = ages.findLast(checkAge); //20 |
The findLastIndex() method finds the index of the last element that satisfies a condition.
1 2 |
const temp = [27, 28, 30, 40, 42, 35, 30]; let pos = temp.findLastIndex(x => x > 40); // 4 |
Hashbang Grammer
This feature would enable us to use Hashbang in some CLI. Hashbang is represented by #! and is a special line at the beginning of the script that tells the operating system which interpreter to use when executing the script.
1 2 3 4 |
#!/usr/bin/env node 'use strict'; console.log(5*4); |
#!/usr/bin/env node this line would invoke a Node.js source file directly, as an executable in its own.
Symbols as WeakMap keys
This allows using unique Symbols as keys. Objects are used as keys for WeakMaps because they share the same identity aspect. Symbol is the only primitive type in ECMAScript that allows unique values therefor using Symbol as key instead of creating a new object with WeakMap.
1 2 3 4 5 6 7 |
const weak = new WeakMap(); const key = Symbol('test'); const myObject = { a:1 }; weak.set(key, myObject); console.log(weak.get(key)); |
Change Array by Copy
The toReversed() method as a safe way to reverse an array without altering the original array. The difference between the new toReversed() method and the old reverse() method is that the new method creates a new array, keeping the original array unchanged, while the old method altered the original array.
1 2 3 4 5 |
const months = ["Jan", "Feb", "Mar", "Apr"]; const reversed = months.toReversed(); console.log(reversed); //["Apr","Mar","Feb","Jan"]; console.log(months); //["Jan", "Feb", "Mar", "Apr"]; |
The toSorted() method as a safe way to sort an array without altering the original array. The difference between the new toSorted() method and the old sort() method is that the new method creates a new array, keeping the original array unchanged, while the old method altered the original array.
1 2 3 4 5 |
const months = ["Jan", "Feb", "Mar", "Apr"]; const sorted = months.toSorted(); console.log(sorted); //["Apr","Feb","Jan", "Mar"]; console.log(months); //["Jan", "Feb", "Mar", "Apr"]; |
The toSpliced() method as a safe way to splice an array without altering the original array. The difference between the new toSpliced() method and the old splice() method is that the new method creates a new array, keeping the original array unchanged, while the old method altered the original array.
1 2 3 4 5 |
const months = ["Jan", "Feb", "Mar", "Apr"]; const spliced = months.toSpliced(0, 1); console.log(spliced); //["Feb", "Mar", "Apr"]; console.log(months); //["Jan", "Feb", "Mar", "Apr"]; |
The with() method as a safe way to update elements in an array without altering the original array.
1 2 3 4 5 |
const months = ["Jan", "Feb", "Mar", "Apr"]; const withArray = months.with(2, "March"); console.log(withArray); //["Jan","Feb", "March", "Apr"]; console.log(months); //["Jan", "Feb", "Mar", "Apr"]; |
To see which ES2023 features are supported by Nodejs, visit https://node.green/. You can also checkout the features of ES2022.