Last year I wrote an article about the new features of ES2022 this year let's check out the new features coming in as part of ES2023.
Features of ES2023
1. Find array from the last
This function will allow us to find element from the last to first of the array based on a condition.
const array = [{a: 1, b: 1}, {a: 2, b: 2}, {a: 3, b: 3}, {a: 4, b: 4}]
console.log(array.findLast(n => n)); //result -> {a: 4,b: 4 }
console.log(array.findLast(n => n.a * 5 === 20)); // result -> {a:4,b:4} as the condition is true so it returns the last element.
console.log(array.findLast(n => n.a * 5 === 21)); //result -> undefined as the condition is false so return undefined instead of {a:4,b:4}.
console.log(array.findLastIndex(n => n.a * 5 === 21)); // result -> -1 as the condition is not justified for returning the last element.
console.log(array.findLastIndex(n => n.a * 5 === 20)); // result -> 3 which is the index of the last element as the condition is true.
2. Hashbang Grammer
This feature would enable us to use Hashbang / Shebang in some CLI.
Shebang 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.
#!/usr/bin/env node
// in the Script Goal
'use strict';
console.log(2*3);
#!/usr/bin/env node
// in the Module Goal
export {};
console.log(2*2);
#!/usr/bin/env node
this line would invoke a Node.js source file directly, as an executable in its own.
We would not need this line (#!/usr/bin/env node)
to invoke a file explicitly via the node interpreter, e.g., node ./file
3. Symbols as WeakMap keys
This allows using unique Symbols as keys. Currently WeakMaps are limited to only allow objects 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.
const weak = new WeakMap();
const key = Symbol('my ref');
const someObject = { a:1 };
weak.set(key, someObject);
console.log(weak.get(key));
More use cases related to ShadowRealms and Record & Tuples using Symbols as WeakMaps
4. Change Array by Copy
This provides additional methods on Array.prototype
to make changes to array by returning new copy of it with the change instead of updating the original array.
New Array.prototype
introduced functions are:
Array.prototype.toReversed()
Array.prototype.toSorted(compareFn)
Array.prototype.toSpliced(start, deleteCount, ...items)
-
Array.prototype.with(index, value)
const numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9]
/* toReversed */
const reversed = numbers.toReversed();
console.log("reversed", reversed); // "reversed", [9, 8, 7, 6, 5, 4, 3, 2, 1]
console.log("original", numbers); // "original", [1, 2, 3, 4, 5, 6, 7, 8, 9]
/* toSorted */
const sortedArr = numbers.toSorted();
console.log("sorted", sortedArr); // "sorted", [1, 2, 3, 4, 5, 6, 7, 8, 9]
console.log("original", numbers); // "original", [1, 2, 3, 4, 5, 6, 7, 8, 9]
/* with */
const replaceWith = numbers.with(1, 100);
console.log("with", replaceWith); // "with", [1, 100, 3, 4, 5, 6, 7, 8, 9]
console.log("original", numbers); // "original", [1, 2, 3, 4, 5, 6, 7, 8, 9]
/* toSpliced */
const splicedArr = numbers.toSpliced(0, 4);
console.log("toSpliced", splicedArr); // "toSpliced", [5, 6, 7, 8, 9]
console.log("original", numbers); // "original", [1, 2, 3, 4, 5, 6, 7, 8, 9]
These are some awesome set of features coming up with ES2023 which I am really looking forward to try them out personally in my day to day coding.
Happy Coding! 👩💻
Top comments (17)
Awesome - finally a functional version of
sort
. I knowsort
returns the sorted array already, but it also mutates the array which I've seen catch some people out a few times.Thanks for the roundup!
Totally. This is the major update I was really waiting for
It's exciting to see how JavaScript continues to evolve and refine its toolbox with every new edition. And I must say, the new sorted method from ES2023 that you've highlighted really caught my eye.
The new
Array.prototype.toSorted(compareFn)
method is quite the game-changer. Sorting arrays has always been a staple in programming, but JavaScript's previous method of sorting an array (Array.prototype.sort()
) alters the original array, which can sometimes lead to unintended side effects.This new toSorted method provides a more functional approach by returning a new sorted array and leaving the original array untouched. I love this because it aligns more with the principle of immutability - a key concept in functional programming. This makes our code more predictable and easier to debug.
And don't forget, this isn't just for number arrays. With the
compareFn
parameter, we can sort arrays of strings, objects, or any other complex data structures.Here's a quick example:
As you can see, the original words array is not modified, promoting more predictable behavior in our code.
Thanks for sharing this article - ES2023 is going to bring some really neat enhancements to our JavaScript toolset. Can't wait to incorporate them into my workflow! 🚀👨💻
It's nice, but the effect has been available for a long time with
const sortedCities = [...cities].sort();
In both cases you're making a copy of the array then sorting it.
Absolutely,
[...cities].sort()
has been a reliable approach.However, the key benefit of
toSorted()
lies in reducing potential bugs. In my experience, I've debugged issues caused by inadvertent array mutations, often from newer developers not fully aware of.sort()
's mutating behavior.toSorted()
provides an intuitive, safer option that could save debugging time in the long run.Progress never stops in the JavaScript world, does it?
Agreed, it's only for the better to provide replacements for some of JS's cruftier artifacts, like in-place mutation. I don't suppose the old methods will ever be deprecated and eventually removed - there's just too much legacy code out there that depends on them.
I really like the new array prototype methods, I try my best to avoid mutating variables in certain situations and sorts are always the worst to deal with.
Also, Ecmascript is a language specification, (2) has nothing to do with the language itself. Node.js has supported skipping shebang lines for at least a decade now, probably since its inception.
Сongratulations 🥳! Your article hit the top posts for the week - dev.to/fruntend/top-10-posts-for-f...
Keep it up 👍
Thank you supporting and sharing the article!🙌
I actually like this new features!
Thanks for sharing.
Massive!! Thank you!
A much better demo for
Array.findLast()
You can more clearly find that the result is the last one object.
great 🙂