7 amazing new JavaScript features in ES14 (2023)
The world’s most popular programming language just got a new update.
Yes, JavaScript:
Ever since 2015, a new JavaScript version has come out every year with tons of powerful features to make life much easier, and 2023 has been no different. Let’s look at what ES14 has to offer.
1. Array
toSorted()
method
Sweet syntactic sugar.
ES14 comes with a new toSorted()
method that makes it easier to sort an array and return a copy without mutation.
So instead of doing this:
const nums = [5, 2, 6, 3, 1, 7, 4];
const clone = [...nums];
const sorted = clone.sort();
console.log(sorted); // [1, 2, 3, 4, 5, 6, 7]
We now get to do this:
const nums = [5, 2, 6, 3, 1, 7, 4];
const sorted = nums.toSorted();
console.log(sorted); // [1, 2, 3, 4, 5, 6, 7]
console.log(nums); // [5, 2, 6, 3, 1, 7, 4]
And just like sort()
, toSorted()
takes a callback function that lets you decide how the sort should happen - ascending or descending, alphabetical or numeric.
const nums = [5, 2, 6, 3, 1, 7, 4];
const sorted = nums.toSorted((a, b) => b - a);
console.log(sorted); // [7, 6, 5, 4, 3, 2, 1
2. Hashbang grammar
What’s a hashbang?
Basically, it’s a sequence of characters in a file that tells the Linux Shell what interpreter or engine it should use to run the file. For example:
codingbeauty.shCopied!
#!/bin/bash
echo "How are you all doing today?"
With this hashbang, we can now easily run codingbeauty.sh
directly in the shell, after using the chmod
command to make the script executable.