6 JavaScript Features to improve your JavaScript skills in 2022
Dev By RayRay
Posted on June 1, 2022
Recently all major browsers updated their JavaScript features. So in this post, I will dive into these six features worth mentioning to improve your JavaScript skills in 2022.
These features are both very new features and features that improve the already existing functionality in JavaScript.
1. Get JavaScript Array item with Array.at()
Before
Lets start with the Array.at() method. Since the early days of JavaScript, we have been using this syntax to get a specific element with a known index from an Array.
After
For now, this works perfectly fine. But array.at()
can be more readable. So with this method, we can write the code from above in a more readable manner.
The syntax becomes shorter, especially with the last or nth-last elements.
If the Array index doesnt exist, you still get an undefined
value back, so nothing has changed there.
When the code doesn't run, select the newest Node version π or try it on Runkit
Browser support
The browser support is perfect, in my opinion. Hopefully, you dont need to support old Internet Explorer browsers because they lack support. Find more information and examples in de MDN Web Docs.
2. Deep copy a JavaScript Object with structuredClone()
If you want to create a copy of a JavaScript Object, it becomes a shallow copy most of the time.
Spread operator copy
This means that updating a nested property (not a top-level property) will also affect the shallow copy.
When the code doesn't run, select the newest Node version πor try it on Runkit
JSON Parse & Stringify
Making deep copies require something more. We need JSON.parse(JSON.stringify(object))
for that. It feels like a hack, but it gets the job done.
When you run this code, you will see that the original myBrowser
is being updated, but the deep copy myBrowserCopy
is not updated. So with JSON.parse(JSON.stringify(object))
you can create deep copies.
When the code doesn't run, select the newest Node version π or try it on Runkit
StructuredClone()
You can use a more straightforward method to create deep copies of your objects.
As you can see, this method has better readability because it says you make a clone of that object.
When the code doesn't run, select the newest Node version πor try it on Runkit
Browser support
The browser support is great, in my opinion. Hopefully, you dont need to support old Internet Explorer browsers because they lack support. Also, keep in mind that some browsers dont support this method for workers. Find more information and examples in de MDN Web Docs.
3. Top-level await
Since ES2017, we have async/await for synchronously writing Promises.
Before
The only condition there was to use await
in JavaScript. You needed to make your function async
, which is sometimes a bit of a hassle. Because you dont want to write async
before every function that uses await
, right?
When the code doesn't run, select the newest Node version πor try it on Runkit
Writing a iffe for every time you wanted to use await
is also pretty ugly π€. When the code doesn't run, select the newest Node version π
After
Well, since now we can use await
without using async
πͺ
When the code doesn't run, select the newest Node version πor try it on Runkit
Now you dont need any iffe boilerplate anymore π₯. We need to write await
; thats it π! Remember that methods in Classes still need to have the async
keyword before it; otherwise, it wont work.
Browser support
In my opinion, the browser support is great. Hopefully, you dont need to support old Internet Explorer browsers because they lack support. Find more information and examples in de V8 documentation.
4. For await of
I dont know if this use case ever happened to you, but for me, it did.
Imagine you need to make multiple AJAX calls after each other, but you want to loop over them. But during the loop, those Promises are not resolved yet. So what are you going to do?
Before
A while ago, it was only possible to wait until all those Promises were resolved. After the resolvent, you could loop over them.
When the code doesn't run, select the newest Node version πor try it on Runkit
While running this code, you can see that if one of the Promises will not be resolved but rejected, the for-loop doesnt even begin to loop over them.
After
But thanks to the for await...of
you can combine a for-loop with the Promise.all()
functionality.
When the code doesn't run, select the newest Node version πor try it on Runkit
As you can see, this is better to read in my opinion. And every time a Promise is resolved, the loop goes to the following Promise, which is excellent!
But when a Promise gets rejected, the for-loop will stop. If you want the loop to continue when a Promise is rejected, you need to use Promise.allSettled()
. With this method, you can see which promises are rejected and fulfilled. (Check MDN Web Docs for more information about Promise.allSettled)
When the code doesn't run, select the newest Node version πor try it on Runkit
Browser support
In my opinion, the browser support is great. Hopefully, you dont need to support old Internet Explorer browsers because they lack support. Find more information and examples in de MDN Web Docs.
5. Private class fields
Every developer that spends some time in TypeScript knows the private
keyword. This tells that a property or method is only used inside that class. But in the browser, you can see that those fields and methods are exposed just like the public ones.
From now on, we can make a property of method private by putting a # before it. Its not only syntactic sugar, but it doesnt expose that field or method to the outside.
class MyCoolClass {
publicField = 'This fields is visible outside the class';
#privateField = 'This field is hidden outside the class';
getPrivateField() {
return this.#privateField;
}
}
const myClass = new MyCoolClass();
console.log('myClass :', myClass);
// myClass : MyCoolClass {
// publicField: "This fields is visible outside the class"
/// #privateField: "This field is hidden outside the class"
// [[Prototype]]: Object
// constructor: class MyCoolClass
// getPrivateField: getPrivateField()
console.log('myClass.publicField :', myClass.publicField);
// myClass.publicField : This fields is visible outside the class
console.log('myClass.#privateField :', myClass.#privateField);
// Uncaught SyntaxError: Private field '#privateField' must be declared in an enclosing class
console.log('myClass.getPrivateField():', myClass.getPrivateField());
// 'This field is hidden outside the class'
Since Runkit doesn't support the private class fields I used Replit for this example.
If you log the whole class in the console, you can see that the private field exists, but when you try to call it, you receive a syntax error. Private fields can be exposed outside the class with a public method.
Browser support
In my opinion, the browser support is great. Hopefully, you dont need to support old Internet Explorer browsers because they lack support. Find more information and examples in de MDN Web Docs.
6. Object.hasOwn
Sometimes we like to check if an Object has a specific property before we try to access it. Yes, I know that there is something like optional chaining π.
If you have to check these things a lot of times, please consider TypeScript. Follow my TypeScript for Beginners guide.
For years we have the Object.prototype.hasOwnProperty()
method in JavaScript. This method returns a boolean when you use it.
When the code doesn't run, select the newest Node version πor try it on Runkit
But when we try to make an Object like this, it will become confusing.
When the code doesn't run, select the newest Node version πor try it on Runkit
Because usually, when you create an Object (const obj = {}
), that Object gets all default properties and methods from Object.prototype
, but when you give null
as a value to the create method, it wont receive anything from Object.prototype
so thats why the hasOwnProperty
method isnt on that Object.
With the Object.hasOwn
, you dont have that problem.
When the code doesn't run, select the newest Node version πor try it on Runkit
Browser support
Note:
Object.hasOwn()
is intended as a replacement for[Object.hasOwnProperty()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/hasOwnProperty)
.
In my opinion, the browser support is great. Hopefully, you dont need to support old Internet Explorer browsers because they lack support. Find more information and examples in de MDN Web Docs.
Thanks!
After reading this story, I hope you learned something new or are inspired to create something new! π€ If so, consider subscribing via email (scroll to the top of this page) or follow me here on Hashnode.
Did you know that you can create a Developer blog like this one, yourself? It's entirely for free. ππ°ππ₯³π₯
If I left you with questions or something to say as a response, scroll down and type me a message. Please send me a DM on Twitter @DevByRayRay when you want to keep it private. My DM's are always open π
Posted on June 1, 2022
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.