ECMA 2021-2023 new features round-up: new operators, array methods and more
18 Oct 2023
Here's a round-up of some features that have landed in JavaScript in the last few years, thanks to ECMAScript editions 12 onwards, that you may be unaware of.
12th edition (2021) 🔗
New assignment operators 🔗
||=
- assigns only if the left side is falsy&&=
assigns only if the left side is truthy??=
assigns only if the left side is nullish (i.e. null or undefined)
So for example:
The variable is created but has a value of undefined. Since undefined equates to falsy, our update to the variable succeeds.
Of course, there's various ways to do this in JavaScript. Assuming let foo;
, all of the following are equivalent:
Promise.any() 🔗
See my dedicated article on Promise.any()
.
String.prototype.replaceAll() 🔗
This method works like its replace()
counterpart except that, if you pass it a string not a RegExp, it replaces all instances not just the first.
Because the method is intended solely for global replacement, if you pass a RegExp to it then you must use the global g
flag, otherwise it'll raise an error. (For this reason it makes little sense to use this method with a RegExp - you might as well stick with replace()
and save three bytes!)
Numeric literal separator 🔗
You can now format numeric literals with underscore separators for easier reading.
The underscore can appear anywhere in the number (except at the start or end) - it's purely syntax sugar and does not affect numerical operation:
Since it's purely a visual thing, you can even use multiple underscores:
Here's some more info on this feature.
13th edition (2022) 🔗
Top-level await 🔗
Normally you can use the await
keyword only in an asynchronous function. Now, though, you can use await
in the top level of your code, provided you're in a module.
Check out my in-depth guide to async/await
Private class members 🔗
This one was a long time coming and is present in just about all other languages. We've had workarounds for years (mostly using constructors), but finally there's an official implementation for private properties, methods, statics etc. in your classes, just by prefixing them with #
.
class Foo {
#bar = 'secret!';
getBar() { return this.#bar; }
}
const inst = new Foo();
inst.getBar(); //"secret!"
Private members are accessible only from within the body of the class itself, not outside, and not even within any sub-classes that extend the class.
It's important to note that private members are exempt from prototypal inheritance, since they cannot be inherited and are accessible only within the class to which they belong.
[string|array].prototype.at() 🔗
This new method for both strings and arrays is simply a means of grabbing a specific character (strings) or member (arrays) denoted by a passed index.
This is equivalent to:
Object.hasOwn() 🔗
This is intended as a replacement for Object.prototype.hasOwnProperty
, because it works on null-prototype objects i.e. those created via Object.create(null)
.
It works in the same way, except the first argument is the object to consider.
const myObj = {foo: 'bar'};
Object.hasOwn(myObj, 'foo'); //true
myObj.hasOwnProperty('foo'); //equivalent
14th edition (2023) 🔗
Array copy methods 🔗
First up we got a bunch of array methods which - and I don't know who was asking for this - copy an array while performing a mutation on it, leaving the original unchanged.
The new methods are:
toSorted()
- likesort()
toReversed()
- likereverse()
with()
- update member at specific index
So for example:
const arr = [2, 3, 1];
const arr2 = arr.toSorted(); //[1, 2, 3]
const arr3 = arr.toReversed(); //[1, 3, 2]
const arr4 = arr.with(0, -2); //[-2, 3, 1]
And to prove that the derived arrays are new arrays, not mutated versions of the original:
New array find last methods 🔗
Finally, there's a few new methods to find the last value (or index of a value) in an array:
findLast()
- equivalent offind()
findLastIndex()
- equivalent offindIndex()
They work the same in that they take a callback function, but these methods count backwards from the end of the array and quit when they find a match.
And that's it! I hope you found this useful.
Did I help you? Feel free to be amazing and buy me a coffee on Ko-fi!