π‘ JavaScript ES14: The New Kid on the Block π¦
ES14 or ECMAScript 2023 is the latest version of JavaScript.
It was released in June 2023.
As Brendan Eich, the creator of JavaScript, once said:
βAlways bet on JavaScript.β
It has four new features:
Searching arrays from the end π
This feature lets us find an element or its index from the end of an array. For example:
let nums = [2, 4, 6, 8, 9, 10];
let lastOddNum = nums.findLast(n => n% 2 === 1); // 9
let lastOddNumIndex = nums.findLastIndex(x => x % 2 === 1); // 4
Introduction of Hashbang Grammar π
Hashbang is a character sequence (#!
) that tells the OS which interpreter to use for a script. For example:
#!/usr/bin/env node
console.log("Hello JS ES 14!");
The different interpreters allowed are NodeJS, PERL, PHP, Python and Ruby. In above script we are using βNodeJSβ as interpreter name.
Hashbang was not supported by JavaScript until ES14.
Symbols can now be WeakMap keys π
Symbols are unique, unchangeable identifiers for object properties.
They let you attach data to objects without altering the object or causing memory leaks, making them ideal for adding metadata to objects outside your control.
For example:
let weakMap = new WeakMap();
let symbolRef = Symbol("sym");
weakMap.set(symbolRef, {value: 121212});
console.log(weakMap.get(symbolRef)); // {value: 121212}
Modifying arrays by copying π
This feature introduces four new methods: with
, toSorted
, toReversed
, and toSpliced
that modify an array and return a new one, leaving the original array unchanged.
For example:
let originalScientists = ["Raman", "Bose", "Kalam", "Chandrasekhar"];
let modifiedScientists = originalScientists.with(0, "Sarabhai").toSorted().toSpliced(1, 2, "Bhabha", "Rao").toReversed();
console.log(modifiedScientists); // ["Sarabhai", "Rao", "Bhabha", "Bose"]
console.log(originalScientists); // ["Raman", "Bose", "Kalam", "Chandrasekhar"]
Conclusion
ES14 adds new JavaScript features. Theyβre not universally supported yet, but can be used with transpilers or polyfills.
Itβs been a wild ride, right? But donβt worry, weβve got some cooler stuff coming up. π€
Happy coding! Stay tuned! π
Bonus Tip: Check out my React 2023 Refresher YouTube videos here.
In Plain English
Thank you for being a part of our community! Before you go:
- Be sure to clap and follow the writer! π
- You can find even more content at PlainEnglish.io π
- Sign up for our free weekly newsletter. ποΈ
- Follow us: Twitter(X), LinkedIn, YouTube, Discord.
- Check out our other platforms: Stackademic, CoFeed, Venture.