DEV Community

Cover image for Top 10 Javascript Features You Can Use in 2024
Aaron Reddix
Aaron Reddix

Posted on

Top 10 Javascript Features You Can Use in 2024

Hey JavaScript Enthusiasts! This article is all about the latest and greatest features in our beloved scripting language, JavaScript. Whether you're a seasoned developer or just dipping your toes into coding, these updates are sure to enhance your experience. Let's jump into the top JavaScript features you can start using today!

1. Optional Chaining

No more long chains of property access with null checks! Optional chaining allows you to safely access deeply nested properties.

const user = { profile: { bio: { name: 'Jane Doe' } } };
console.log(user?.profile?.bio?.name); // Outputs: Jane Doe
Enter fullscreen mode Exit fullscreen mode

2. Nullish Coalescing Operator

Avoid those pesky null or undefined values with the nullish coalescing operator (??). It lets you set default values only if the left-hand side is null or undefined.

const userInput = null;
const username = userInput ?? 'Guest';
console.log(username); // Outputs: Guest
Enter fullscreen mode Exit fullscreen mode

3. BigInt

Handling large integers in JavaScript has never been easier. BigInt lets you work with numbers larger than the Number type's safe integer limit.

const bigNumber = 9007199254740991n + 1n;
console.log(bigNumber); // Outputs: 9007199254740992n
Enter fullscreen mode Exit fullscreen mode

4. Dynamic Import

Load modules dynamically at runtime with dynamic imports, which help optimize loading times and resources.

if (condition) {
  import('./module.js').then((module) => {
    module.default();
  });
}
Enter fullscreen mode Exit fullscreen mode

5. Promise.allSettled

Handle multiple promises and get the results of each promise, regardless of whether they were fulfilled or rejected.

const promises = [fetch('/api'), fetch('/other-api')];
Promise.allSettled(promises).then((results) =>
  results.forEach((result) => console.log(result.status))
);
Enter fullscreen mode Exit fullscreen mode

6. Private Class Fields

Keep your class internals private with private class fields. These are accessible only within the class.

class MyClass {
  #privateField = 42;

  getPrivateField() {
    return this.#privateField;
  }
}

const myInstance = new MyClass();
console.log(myInstance.getPrivateField()); // Outputs: 42
Enter fullscreen mode Exit fullscreen mode

7. Logical Assignment Operators

Combine logical operators with assignment in a shorter, more readable syntax.

let a = 0;
a ||= 1; // a becomes 1 if it's falsy
console.log(a); // Outputs: 1
Enter fullscreen mode Exit fullscreen mode

8. String.prototype.replaceAll

Easily replace all occurrences of a substring in a string with replaceAll.

const text = 'Hello World! Hello Universe!';
const newText = text.replaceAll('Hello', 'Hi');
console.log(newText); // Outputs: Hi World! Hi Universe!
Enter fullscreen mode Exit fullscreen mode

9. WeakRefs

Create weak references to objects, preventing them from being garbage-collected.

let obj = { data: 'important' };
const weakRef = new WeakRef(obj);
obj = null; // obj can now be garbage-collected
Enter fullscreen mode Exit fullscreen mode

10. Top-Level Await

Use the await keyword at the top level of your modules, simplifying asynchronous code.

const data = await fetch('/api').then((res) => res.json());
console.log(data);
Enter fullscreen mode Exit fullscreen mode

Wrapping Up

2024 is looking bright for JavaScript developers! With these new features, you'll write cleaner, more efficient, and more readable code. So update your tools and start exploring these awesome updates in web development.

Keep coding and having fun! Until next time, happy coding! πŸš€

Top comments (2)

Collapse
 
kocreative profile image
Kat

Thank you for the write up!

Collapse
 
aaronreddix profile image
Aaron Reddix

Your welcome, Kat!