DEV Community

Mercy
Mercy

Posted on

This week Javascript 2

JavaScript continues to evolve. The latest significant update, ECMAScript 2023 (ES14), was released in June 2023. This update introduced several new features that enhanced the language's functionality and improved developer efficiency.

Key Features of ECMAScript 2023

1. Top-Level Await
The introduction of top-level await allowed developers to use the await keyword at the top level of modules, simplifying asynchronous code without needing to wrap it in an async function.

// Using top-level await
const data = await fetch('https://api.example.com/data');
const jsonData = await data.json();
console.log(jsonData);
Enter fullscreen mode Exit fullscreen mode

2. New Array Methods
ECMAScript 2023 added several new methods for array manipulation that do not mutate the original array:

  • toSorted(): Returns a new sorted array.
  • toReversed(): Returns a new array with elements in reverse order.
  • toSpliced(): Returns a new array with elements removed or added without mutating the original.

Example:

const numbers = [3, 1, 4, 1, 5];

// Using toSorted
const sortedNumbers = numbers.toSorted();
console.log(sortedNumbers); // [1, 1, 3, 4, 5]

// Using toReversed
const reversedNumbers = numbers.toReversed();
console.log(reversedNumbers); // [5, 1, 4, 1, 3]

// Using toSpliced
const splicedNumbers = numbers.toSpliced(1, 2); // Remove two elements starting from index 1
console.log(splicedNumbers); // [3, 5]
Enter fullscreen mode Exit fullscreen mode

3. findLast() and findLastIndex()
These methods allow you to find the last element or index that satisfies a certain condition without needing to reverse the array first.

Example:

const numbers = [5, 12, 50, 130, 44];

// Using findLast
const lastGreaterThan40 = numbers.findLast(num => num > 40);
console.log(lastGreaterThan40); // 130

// Using findLastIndex
const lastIndexGreaterThan40 = numbers.findLastIndex(num => num > 40);
console.log(lastIndexGreaterThan40); // 3 (index of 130)
Enter fullscreen mode Exit fullscreen mode

4. RegExp Match Indices API
This feature enhances regular expressions by providing the start and end indices of matches in a string.

Example:

const regex = /(foo)/g;
const str = 'foo bar foo baz';
const matches = [...str.matchAll(regex)];

for (const match of matches) {
    console.log(`Match: ${match[0]}, Indices: ${match.indices[0]}`);
}
// Output:
// Match: foo, Indices: [0, 3]
// Match: foo, Indices: [10, 13]
Enter fullscreen mode Exit fullscreen mode

5. Error Cause Extension
This feature allows developers to provide additional context when throwing errors by attaching a cause property.

Example:

try {
    throw new Error('Something went wrong', { cause: 'Invalid input' });
} catch (error) {
    console.error(error.message); // Something went wrong
    console.error(error.cause); // Invalid Input
}
Enter fullscreen mode Exit fullscreen mode

Looking Ahead: ECMAScript 2024
As we look towards ECMAScript 2024 (ES15), expected features include:

  • Temporal API for improved date and time handling.
  • Realms API for better security and isolation in JavaScript environments.
  • Immutable Data Structures such as Records and Tuples.
  • Advanced Pattern Matching for more efficient data searching.
  • Decorator Syntax for enhancing classes and methods.

These upcoming features aim to further streamline development processes and enhance code clarity and safety.

To summarise, ECMAScript 2023 brings significant enhancements that improve how developers interact with arrays, handle asynchronous operations, manage errors, and work with regular expressions.

Top comments (3)

Collapse
 
ewaldhorn profile image
Ewald

All these advances are great, do you know how well-supported these features are out in the wild? Do you have a strategy to support older JavaScript environments that do not have these advanced features yet?

Collapse
 
devmercy profile image
Mercy

There are several strategies developers can use to ensure compatibility with older JavaScript environments such as
Transpilation
Using transpilation tools like Babel can convert modern JavaScript syntax into equivalent code that works in older browsers and environments.
Again using Pollyfills can help

I have a post on that tomorrow on This week Javascript 3

Collapse
 
devmercy profile image
Mercy

If you have any further questions, please don't hesitate to ask.