DEV Community

Sergio Mesa
Sergio Mesa

Posted on

Exploring the New Features of ECMAScript 2024 (ES15)

Image description

Hi, developers! Today, we’ll dive into the new features of ECMAScript 2024 (ES15). This version includes several improvements and new functions that will help you write cleaner, more efficient, and maintainable code.

JavaScript Version Numbers

Starting in 2015, ECMAScript versions are numbered annually: ES6, ES7, etc. The 2024 version, known as ES15, will be officially released in June 2024. This annual numbering allows us to closely follow JavaScript’s continuous evolution and stay updated on the latest improvements and features.

New Features in ES2024

Object.groupBy()

The Object.groupBy() function allows grouping object elements based on values returned by a callback function. This is especially useful for organizing data intuitively. Here’s an example of grouping fruits by quantity:

const fruits = [
  { name: 'apple', quantity: 10 },
  { name: 'banana', quantity: 20 },
  { name: 'cherry', quantity: 10 }
];

const groupedByQuantity = Object.groupBy(fruits, fruit => fruit.quantity);
console.log(groupedByQuantity);
// {
//   '10': [{ name: 'apple', quantity: 10 }, { name: 'cherry', quantity: 10 }],
//   '20': [{ name: 'banana', quantity: 20 }]
// }
Enter fullscreen mode Exit fullscreen mode

Map.groupBy()

Map.groupBy() is similar to Object.groupBy(), but for maps. It allows grouping map elements based on values returned by a callback function, providing additional flexibility when working with more complex data sets.

const fruits = new Map([
  ['apple', 10],
  ['banana', 20],
  ['cherry', 10]
]);

const groupedByQuantity = Map.groupBy(fruits, ([, quantity]) => quantity);
console.log(groupedByQuantity);
// Map {
//   10 => Map { 'apple' => 10, 'cherry' => 10 },
//   20 => Map { 'banana' => 20 }
// }
Enter fullscreen mode Exit fullscreen mode

Differences Between Object.groupBy() and Map.groupBy()

The main difference between Object.groupBy() and Map.groupBy() is that Object.groupBy() groups elements into an object, while Map.groupBy() groups elements into a map. This distinction allows choosing the data structure that best fits your application’s needs.

Temporal API

ES2024 introduces the Temporal API, a series of new objects for handling dates and times more intuitively and accurately. The Temporal API is a significant improvement over existing date and time objects, providing a more robust and flexible way to work with these data.

Temporal.PlainDate

const date = Temporal.PlainDate.from('2024-06-30');
console.log(date.year); // 2024
console.log(date.month); // 6
console.log(date.day); // 30
Enter fullscreen mode Exit fullscreen mode

Temporal.PlainTime

const time = new Temporal.PlainTime(14, 30);
console.log(time.hour); // 14
console.log(time.minute); // 30
Enter fullscreen mode Exit fullscreen mode

Temporal.PlainMonthDay

const monthDay = new Temporal.PlainMonthDay(5, 1);
console.log(monthDay.month); // 5
console.log(monthDay.day); // 1
Enter fullscreen mode Exit fullscreen mode

Temporal.PlainYearMonth

const yearMonth = new Temporal.PlainYearMonth(2024, 6);
console.log(yearMonth.year); // 2024
console.log(yearMonth.month); // 6
Enter fullscreen mode Exit fullscreen mode

The improvements introduced in ECMAScript 2024 reflect the continuous focus on making JavaScript more functional and declarative. These updates not only simplify data handling but also enhance code readability and maintainability.

ECMAScript 2024 marks another significant step in the evolution of JavaScript. The new features and improvements are designed to make development more efficient and enjoyable. As we adopt these new tools, we can expect to write cleaner, more robust, and maintainable code.

For more details and examples on ECMAScript 2024, I recommend visiting the official W3Schools page on ECMAScript 2024. Happy coding!

Top comments (0)