DEV Community

Delia
Delia

Posted on

Exploring the Newest Features in JavaScript ES2024

The JavaScript ecosystem continues to evolve rapidly, with each ECMAScript (ES) release introducing new features that enhance the language's capabilities and developer experience. ES2024 is no exception, bringing a host of exciting features that promise to improve code readability, performance, and overall efficiency. In this article, we'll explore the newest features in JavaScript ES2024 that are ready to be used, complete with practical examples and explanations to help you understand and apply them in your projects.

1. Top-Level await

Top-level await simplifies the use of asynchronous operations at the module level without the need for an async function wrapper.

Example:

// data.js
const response = await fetch('https://api.example.com/data');
export const data = await response.json();

// main.js
import { data } from './data.js';
console.log(data);
Enter fullscreen mode Exit fullscreen mode

Status: Stage 4 (Part of ES2022)

Usage:

Supported in modern browsers and Node.js. It can be used in module scripts to simplify asynchronous code at the top level.

2. WeakRefs and FinalizationRegistry

WeakRefs and FinalizationRegistry provide new ways to manage memory in JavaScript by allowing references to objects that don't prevent those objects from being garbage-collected.

Example:

let registry = new FinalizationRegistry((heldValue) => {
  console.log(`${heldValue} was collected`);
});

let obj = {};
let ref = new WeakRef(obj);
registry.register(obj, 'MyObject');

// Clear the reference to the object
obj = null;

// Simulate garbage collection
setTimeout(() => {
  console.log(ref.deref()); // Output: undefined (if garbage collected)
}, 1000);
Enter fullscreen mode Exit fullscreen mode

Status: Stage 4 (Part of ES2021)

Usage:

Supported in modern browsers and Node.js. It allows developers to manage memory more effectively by using weak references and registering finalization callbacks.

3. Private Instance Methods and Accessors

Building on the class fields and private instance fields introduced in previous ECMAScript versions, ES2024 adds support for private instance methods and accessors. This feature allows developers to define private methods and getters/setters that are only accessible within the class.

Example:

class Person {
  #name;

  constructor(name) {
    this.#name = name;
  }

  #greet() {
    return `Hello, ${this.#name}!`;
  }

  get greeting() {
    return this.#greet();
  }
}

const person = new Person('Alice');
console.log(person.greeting); // Output: "Hello, Alice!"
Enter fullscreen mode Exit fullscreen mode

Status: Stage 4 (Part of ES2022)

Usage:

Available in some JavaScript engines like V8 (used by Chrome and Node.js). It can also be used with transpilers like Babel for broader compatibility.

4. Temporal

The Temporal API is a new, modern API for working with dates and times in JavaScript. It addresses many of the shortcomings of the existing Date API, providing a more comprehensive and user-friendly way to handle date and time manipulation.

Example:

const now = Temporal.Now.plainDateTimeISO();
console.log(now.toString()); // Output: "2024-07-02T14:23:47.123456789"

const birthDate = Temporal.PlainDate.from('1990-01-01');
const age = now.since(birthDate).years;
console.log(age); // Output: 34
Enter fullscreen mode Exit fullscreen mode

Status: Stage 3 (Proposal)

Usage:

Not yet part of the official standard, but available in some experimental builds and polyfills. Keep an eye on its progress to integrate it into projects once it reaches stage 4.

JavaScript ES2024 introduces a range of powerful new features that enhance the language's capabilities and improve developer productivity. From top-level await and private instance methods to the Temporal API and WeakRefs, these features make JavaScript more robust, expressive, and easier to use. By staying up-to-date with these advancements, you can write more efficient and maintainable code. Happy coding with ES2024!

Further Reading

Top comments (2)

Collapse
 
syakirurahman profile image
Syakir

I feel like i know you. Did u work in Deeplynx on 2021?

Collapse
 
delia_code profile image
Delia

Yes I did :D