ECMAScript 2023 Overview
Since 2016, ECMAScript versions are named by year rather than number, culminating most recently in ECMAScript 2023, the 14th edition, published in June 2023.
New Features in ES2023
- Array findLast() and findLastIndex(): Methods for finding elements and their indices from the end of an array.
- Array toReversed(): Creates a reversed copy of an array, leaving the original array unchanged.
- Array toSorted(): Produces a sorted copy of an array without modifying the original.
- Array toSpliced(): Generates a new array with modifications, without altering the original.
- Array with(): Updates an element at a specific index without changing the original array.
- #! (Shebang): Allows scripts to be run using a specified interpreter, directly from the command line.
Examples and Explanation
Array findLast() Method
Returns the last element in an array that meets a specified condition:
const temperatures = [27, 28, 30, 40, 42, 35, 30];const highestAbove40 = temperatures.findLast(temp => temp > 40); // 42
Array findLastIndex() Method
Finds the index of the last element that satisfies a condition:
const temperatures = [27, 28, 30, 40, 42, 35, 30];
const lastIndexAbove40 = temperatures.findLastIndex(temp => temp > 40); // 4
Array toReversed() Method
Creates a reversed copy of the array:
const months = ["Jan", "Feb", "Mar", "Apr"];
const reversedMonths = months.toReversed(); // ["Apr", "Mar", "Feb", "Jan"]
Array toSorted() Method
Sorts the array into a new copy:
const months = ["Jan", "Feb", "Mar", "Apr"];
const sortedMonths = months.toSorted(); // ["Apr", "Feb", "Jan", "Mar"]
Array toSpliced() Method
Creates a new array by splicing without affecting the original:
const months = ["Jan", "Feb", "Mar", "Apr"];
const splicedMonths = months.toSpliced(1, 2); // ["Jan", "Apr"]
Array with() Method
Safely updates an array element:
const months = ["Jan", "Feb", "Mar", "Apr"];
const updatedMonths = months.with(2, "March"); // ["Jan", "Feb", "March", "Apr"]
JavaScript Shebang
Enables scripts to be executed directly with an interpreter specified in the script:
#!/usr/bin/env node//
Now, you can execute this script with ./fileName.js instead of 'node fileName.js'
These additions in ECMAScript 2023 provide JavaScript developers with more functional tools to manage arrays and streamline script execution, enhancing coding efficiency and maintainability. However, newer features may require polyfills to ensure compatibility with older browsers.