Syntax Video
9 NEW JavaScript Features in ES2025
intro
ECMAScript is the standard that defines how the JavaScript language works. It tells us what features should exist, how JavaScript should behave in certain scenarios, what does the syntax look like. It standardizes all of that. And every year, the TC39 committee introduces a new version of the specification that introduces new features to the language. And this year is no different. In this video, I'm going to walk you through the nine new features that have been added to ECMAScript for the 2025 release of the standard. If that sounds good to you, let's dive in. My name is CJ. Welcome to Syntax.
[Music] So, if you take a look at all of the previous versions of ECMAScript, you can see that in modern history, they release a new version of the standard every year in June. And essentially any proposal that has made it to stage 4 before this release of the standard in June will be included in that new version of ECMAScript. And so, this is where I got all of this information. And if you take a look at that same proposals repo, there's a file called finished proposals. And this is a table of every single feature added to the language and the year that it was finalized. So in this table, everything that was finalized to be published in the year 2025 is what we're going to be looking at today. Now the first feature is promise.try.
ECMAScript Versions
Promise.try
And this is useful if you are dealing potentially with functions or libraries from functions that may not return a promise, but maybe sometimes return a promise. You can see that it's baseline 2025 available. It's in all the major browser engines. And essentially, you would want to use this in a scenario where maybe you have a function that doesn't return a promise and you want to like instantly resolve it. This is how you might have done it before, but now with promise.try, you can just pass the function in directly. And it's important to note that this is not equivalent to promise.resolve.in and then passing the function in because this will actually behave asynchronously and this function call will be put into the microtask queue. Whereas with promise.try, try. If this is a synchronous function, it will just be called synchronously. And you can see this a little bit better in this example. So this do something function takes in any kind of function. That function could return a promise or it could just be a regular plain old function. And we can call it with regular functions, functions that throw or async functions. So these are functions that return a promise. But by passing that into promise.try, we get a
consistent behavior. So we know that we can use a dot then on it. We don't have to check to see like if it is a promise. So, this seems super useful, especially if working like with legacy code bases or libraries maybe that are like callback based that haven't really migrated to promises yet. I honestly don't see myself using this as much because most of the time if you're in control of the code, you could just put the async keyword on a function, especially if it has like an early return that isn't awaiting a promise, but then maybe later on it does need to return a promise. Putting the async keyword on there means if this function immediately returns, it's still technically returning a promise. So, I don't really see myself reaching for this anytime soon, but you might actually find a use case for this in your codebase. Now, the next set of features are actually seven methods that have been added to the set prototype.
Set Methods
So, a set is a data structure that allows us to only have unique items in a collection. And you can create a set by passing in an array. So, if you have an array that potentially has duplicate strings in it, you can pass it into a set. That set will actually remove those duplicates. But these methods make it really useful to perform operations between two sets. And if you check out the MDN page, they have some nice vin diagrams that show what these methods actually do. And these methods come from the world of mathematics because there's a thing called set theory. And in mathematics, we work with sets. And all of these method names are from mathematics. So difference, intersection, symmetric difference, union, is disjoint from, is subset of.
But if you look at these vin diagrams, it gives you a good picture of what happens when you perform this operation on two sets. And to really see where this is useful, I have some examples where we're just using some JavaScript arrays. So in this example, we're doing some access control. So let's say we have a list of users that are active in the system and then a list of users that are admins. If I want to figure out which of the admins are active, we could do like a filter. So take all of the active users and then filter any out that are admins. And then if I want to do the reverse of that, I could take all the active users and filter out the ones that are not admins. And then similarly, if I want to find all the admins that are active, I could say g give me all the admin users where every single one is in the active user array. So these are kind of two different ways to get the admins that are active. But you might have come across some code like this in a codebase and um it takes a second for you to kind of understand what's happening. But if we were to use set methods, the active admins are just an intersection of the active users and the admin users. The non-admins is the difference between the active users and
the admin users. And active admins is also where admin users is a subset of active users. Now again, these two things are basically doing the same thing. It's just in the other direction. Uh but if you find yourself doing a lot of array comparison in your app, you could potentially benefit from these set methods by just turning your arrays into sets. Another example of this is synchronizing some data. So maybe you have some data locally and then you also have some data that's coming from a server and you want to know what are the new items that I don't currently have locally. So you might take all the local items, filter out the ones that the remote does not have, and then you know those are the new ones. Similarly, in the remote list, if things have been removed, you might want to filter out the ones that are no longer in there.
You might also want to figure out for common items which ones are both local and remote. And then you might also want to know which ones are completely different from your local and remote array. But again, trying to read all of this code takes a lot of brain power. If we use set methods, it's a lot more straightforward. So the items to add is just the difference between the local items and the remote items. The items to remove is the difference between the remote items and the local items. The common items is the intersection between local and remote. And we can find the difference between the sets by using local items is disjoint from remote items. So these methods take some getting used to, but the code is a lot nicer to read instead of having to figure out all these like filters uh and different things like that that you might be doing in your codebase. Next up, there are three features that have been added to regular expressions. The first one being reax.escape. So this adds a method to the global reax object where you can pass in a string that might have things that are like special characters in regular expressions and it will automatically escape them for you.
RegExp.escape
So for instance, if you pass in this sentence that says buy it, use it, break it, fix it. These periods, if you didn't escape them, would be interpreted as the regular expression token that matches on anything. But let's say you're dealing with some user provided data, like maybe a user types into a search form and then you want to pass that into a regular expression. Typically, you had to reach for a library or manually write some code that would find all the characters that might be interpreted as regular expression tokens and then escape them.
Rejax.escape does this automatically. So, this is something that people have been asking for for a very long time. Now, you don't have to install a separate library. It's super easy to just take some user provided input, escape it, and then pass it into a regular expression. The next regular expression feature is called regular expression pattern modifiers. So, pattern modifiers already exist. So for instance, you can create a regular expression and then pass in I on the end of it and this will be a case insensitive regular expression. If you look on MDN, they have all the various flags you can pass in. You could do G for a global search. You could do M for a multi-line search or use S that will allow periods to match on newline characters. So there's several different flags that you can pass in. But what the modifiers feature allows us to do is pass in these global flags that will be applied to the whole regular expression, but then within the regular expression, we can disable some of those flags. So this whole expression is case insensitive but for this one matching group it is not case sensitive meaning the second match here has to be lowercase a toz instead of also
RegExp Pattern Modifiers
including uppercases and similarly if you don't have a flag on the end here you can just make a subsection of your regular expression be case insensitive. So this is really nice. Before you used to have to create like multiple regular expressions or get much more specific with how you define the matching groups, but with this you can take an existing one and just turn off or turn on those modifiers inside of groups. And the last feature added to regular expressions is duplicate named capturing groups. Now you might not know this, but you can actually create named groups inside of your regular expression. So this regular expression says if you match four digits at the beginning of this incoming string that's going to be in a group called year and then after that we're matching two more digits or we might see two digits four digits. So this is a regular expression that would match on dates either the four-digit year- the month or the month dash the four-digit year. And in this case you can see that we're putting a group name on the year here.
RegExp Duplicate Named Capture Groups
But before this feature was added this would actually throw a syntax error because we had the group year repeated twice. And if you look on the MDN page, it actually hasn't been updated for the latest spec yet because there is this caveat here that says all names must be unique within the same pattern. Multiple named capturing groups with the same name would result in a syntax error. So right now, if you were to run this code on a JavaScript engine that hasn't been updated yet, this wouldn't even work because we have the word year twice. But now with this new addition, we can use the same capture group name in multiple places inside of a regular expression.
Import Attributes / JSON Modules
Now, these next two features deal with importing things into our JavaScript codes. And it was originally just a single proposal, but it's actually been broken out into two. So, import attributes you might have seen, especially if you've worked in TypeScript code, where you want to import a JSON file and then you use the width assertion. This actually formally adds it to the JavaScript language. And it also works with dynamic imports.
Essentially, we can pass in the options there. But where this is really interesting is supporting things beyond JSON. So this formalizes the syntax and we also have the proposal that specifically allows for JSON modules and all of the engines now support type of JSON. But this sets us up so that in the future we could have type CSS or type HTML. And so imagine being able to import like an HTML partial and using it in your JavaScript code or being able to import CSS modules directly into JavaScript code without the need of a bundler. Now this was new to me but importing CSS actually works inside of Chrome browser right now. Now, if you look on the MDN page, they have this example where you import a stylesheet from a URL with type CSS. Now, I tried running this inside of Firefox. It says invalid module type, but in Chrome, this actually works. So, I have some example codes here. I just have an HTML file.
I'm bringing in this JavaScript file as type module. And then I have a div with a class of card on the page. And then in my JavaScript, I'm importing this style file. I'm also importing a JSON file. It's just a little object with a message hello world. And then in order to apply these styles to the document, we have to use the adopted stylesheets property on the document and add those styles to it.
And then now that I pulled that data in as well, I'm actually updating the text content on the page. And if we look at the styles, it's just some standard CSS. So I set up light and dark mode and then style this card appropriately. But when this code runs, you can see that it actually adds those styles to the page. Now, where I see this being very useful is with things like web components. So in this case, I'm adding the styles to the entire page, but this also works with the shadowdom. So you could potentially import a CSS file like this and then add it directly to the shadowdom of a web component. Now, like I mentioned, type CSS is only supported inside of Chrome right now, but eventually once this is supported in a lot more places, we'll have much more modular code that's starting to look a lot like the kind of code you can write when you have a bundler. The next new feature is iterator helpers. So iterators in JavaScript are this little star syntax in the yield syntax. So the these are generators and essentially they're functions that can return multiple values. And so if you haven't seen these before, the way it would work is you could invoke this function but this gives you back an iterator and then that iterator if you want to get the
Iterator Helpers
next value that is yielded you call next. So the first time you call naturals you would get zero and then if you call next you would get one and then if you call next you would get two etc. But these iterator helpers basically give us map, filter, reduce, all the things that you're used to using on arrays, you can now use on iterators. So in this case, we add map to this iterator here. And now instead of just returning 1 2 3 4, we're returning the value multiplied by itself. And what's nice about this is because it's an iterator, this map isn't going to just run all at once. Essentially, when you call next, that's when it will evaluate that map function for the value that it's about to provide. So, if you're new to generators, this might seem a little bit weird, but basically, you define the code here, and then it's only called for each new value when we're going to grab that that next value. But like I mentioned, you've got map, you've also got filter. There's also take, which we don't have on arrays. But this is nice because you can see for this generator, it will just go forever, right? It says
while true, and it's just going to you could just call next forever and ever to get all of the natural numbers. But let's say you want to stop at a certain point. take three would make it so that once this generator has been called three times, it marks the iterator as done, so we can't call it anymore. So, this is super useful, especially if you know that this thing runs forever, but you want to limit it to a certain amount. There's also drop, which will skip a certain number of values at the beginning. There's flat map, reduce. So, all these things that you typically would associate with being able to modify arrays, you can now do with iterators, and you can combine them. So, you can do things like take five, reduce, uh, and another really useful one is to array. Okay, so in all of the previous code examples, we had to call next.ext.next.next.
But in this case, if I know that I only need the first five values, and I'm going to put it into an array anyways, I can just call to array that instantly calls the iterator, invokes it five times, and then puts all of the results into an array. So these are awesome. I personally don't work with uh generator functions in my own code, but if you're dealing with certain libraries that under the hood have code like this, or maybe you're dealing with a library that returns a stream of data, all these functions will be super useful. And it actually is very similar and reminds me of Rx.js which is heavily used in the world of Angular where you basically have streams of data and you can perform all these operations on the streams but now we have this directly inside of JavaScript. Now the last feature is one that I probably won't use ever but uh will be useful for some people that are maybe doing some lower level coding and it is the introduction of float 16 array. So before this in JavaScript you could create a float 32 array and so that's an array that holds 32-bit floatingoint numbers. really big numbers and you can hold that inside of arrays.
Float16Array
So, this is super useful if you're doing things that are like graphics related, but also these days if you're doing things that are like AI related, like matrix multiplication and stuff like that, sometimes you need to hold on to some really big numbers. And before the introduction of this, we only had 32-bit floatingoint numbers. And if you needed a 16- bit number, you had to manually convert it or install a library to do it. But now we can directly create arrays of float 16 values. And the other aspect here is it introduces some methods on data view. So data view is an object in JavaScript for working with these arrays of numbers. And now it has methods on it called get float 16 or set float 16. Whereas before it only worked with 32-bit values. So like I said, I probably won't use this as much, but you can bet that a lot of these graphics focused libraries or maybe AI related libraries are going to be using this under the hood. So that's it for all the new features in ES 2025. But I'd love to know which of these features are you already using? Which of these features are you excited about? Which of these features do you think should have been added to the language years ago? Let me know down in the comments. That's all I
Thanks!
Quote it with credit and a link. Quoting Syntax