DEV Community

Dev By RayRay
Dev By RayRay

Posted on • Originally published at hasnode.byrayray.dev on

6 JavaScript Features to improve your JavaScript skills in 2022

Image description

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.

divider-byrayray.png

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.

const array = [0, 1,2,3,4,5,6,7,8,9] const firstElement = array[0]; const fourthElement = array[3] const lastElement = array[array.length - 1]; const thirdLastElement = array[array.length - 3];

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.

const array = [0, 1,2,3,4,5,6,7,8,9] const firstElement = array.at(0); const fourthElement = array.at(3) const lastElement = array.at(-1); const thirdLastElement = array.at(-3);

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.

divider-byrayray.png

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

const myBrowser = { language: 'JavaScript', framework: 'Angular', browser: 'Brave', os: 'Windows 11', date: { time: new Date().getTime(), date: null } } const myBrowserShallowCopy = {...myBrowser}; console.log('before myBrowser:', myBrowser); console.log('before myBrowserShallowCopy:', myBrowserShallowCopy); myBrowserShallowCopy.browser = 'Chrome'; console.log('after update myBrowser:', myBrowser); console.log('after update myBrowserShallowCopy:', myBrowserShallowCopy); myBrowser.date.date = new Date(); console.log('after update original myBrowser:', myBrowser); console.log('after update original myBrowserShallowCopy:', myBrowserShallowCopy);

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.

const myBrowser = { language: 'JavaScript', framework: 'Angular', browser: 'Brave', os: 'Windows 11', date: { time: new Date().getTime(), date: null } } const myBrowserCopy = JSON.parse(JSON.stringify(myBrowser)); console.log('before myBrowser:', myBrowser); console.log('before myBrowserCopy:', myBrowserCopy); myBrowserCopy.browser = 'Chrome'; console.log('after update myBrowser:', myBrowser); console.log('after update myBrowserCopy:', myBrowserCopy); myBrowser.date.date = new Date(); console.log('after update original myBrowser:', myBrowser); console.log('after update original myBrowserCopy:', myBrowserCopy);

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.

const myBrowser = { language: 'JavaScript', framework: 'Angular', browser: 'Brave', os: 'Windows 11', date: { time: new Date().getTime(), date: null } } const myBrowserCopy = structuredClone(myBrowser); console.log('before myBrowser:', myBrowser); console.log('before myBrowserCopy:', myBrowserCopy); myBrowserCopy.browser = 'Chrome'; console.log('after update myBrowser:', myBrowser); console.log('after update myBrowserCopy:', myBrowserCopy); myBrowser.date.date = new Date(); console.log('after update original myBrowser:', myBrowser); console.log('after update original myBrowserCopy:', myBrowserCopy);

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.

divider-byrayray.png

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?

(async function() { const one = () => { return new Promise((resolve) => { setTimeout(() => resolve(2), 2000); }) }; console.log(await one()); }());

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 πŸ’ͺ

const one = () => { return new Promise((resolve) => { setTimeout(() => resolve(2), 2000); }) }; console.log(await one());

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.

divider-byrayray.png

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.

const one = () => { return new Promise((resolve) => { setTimeout(() => resolve(2), 2000); }) }; const two = () => { return new Promise((resolve) => { setTimeout(() => resolve(3), 3000); }) }; const three = () => { return new Promise((resolve) => { setTimeout(() => resolve(5), 5000); }) }; const four = () => { return new Promise((resolve, reject) => { setTimeout(() => reject(6), 6000); }) }; // This async IFFE is only needed in NodeJS (async () => { try { const allPromises = await Promise.all([one(), two(), three(), four()]); for (const result of allPromises) { console.log('result:', result) } } catch (e) { console.log('caught', e); } })();

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.

const one = () => { return new Promise((resolve) => { setTimeout(() => resolve(2), 2000); }) }; const two = () => { return new Promise((resolve) => { setTimeout(() => resolve(3), 3000); }) }; const three = () => { return new Promise((resolve) => { setTimeout(() => resolve(5), 5000); }) }; const four = () => { return new Promise((resolve, reject) => { setTimeout(() => reject(6), 6000); }) }; const arr = () => { return [one(), two(), three(), four()]; } // This is only needed in NodeJS (async () => { try { for await (const result of arr()) { console.log('result:', result) } } catch (e) { console.log('caught', e); } })();

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)

const one = () => { return new Promise((resolve) => { setTimeout(() => resolve(2), 2000); }) }; const two = () => { return new Promise((resolve, reject) => { setTimeout(() => reject(3), 3000); }) }; const three = () => { return new Promise((resolve) => { setTimeout(() => resolve(5), 5000); }) }; // This async IFFE is only needed in NodeJS (async () => { const promisesArr = [one(), two(), three()]; const allPromises = await Promise.allSettled(promisesArr).then((promises) => { for (const result of promises) { console.log('result:', result) } }, (error) => console.error(error)); })();

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.

divider-byrayray.png

5. Private class fields

Every developer that spends some time in TypeScript knows the privatekeyword. 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'

Enter fullscreen mode Exit fullscreen mode

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.

divider-byrayray.png

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.

const obj = { propA: 'Value', propB: false } console.log('propA:', obj.hasOwnProperty('propA')); // "propA: true" console.log('propC:', obj.hasOwnProperty('propC')); // "propA: false"

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.

const obj = Object.create(null); obj.propA = 'Value'; obj.propB = false; console.log('propA:', obj.hasOwnProperty('propA')); // Uncaught TypeError: obj.hasOwnProperty is not a function console.log('propC:', obj.hasOwnProperty('propC')); // Uncaught TypeError: obj.hasOwnProperty is not a function

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.

const obj = Object.create(null); obj.propA = 'Value'; obj.propB = false; console.log('propA:', Object.hasOwn(obj, 'propA')); console.log('propC:', Object.hasOwn(obj, 'propC'));

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.

divider-byrayray.png

Thanks!

hashnode-footer.pngAfter 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 😁

Discussion (0)