25 Must‑Know ES202x Features to Supercharge Your JavaScript Code
This article introduces 25 powerful ECMAScript enhancements—including optional chaining, nullish coalescing, private class fields, dynamic imports, and decorators—explaining their benefits and providing concise code examples to help developers write cleaner, more efficient JavaScript.
JavaScript, as one of the most popular programming languages, continuously evolves through the ECMAScript standard, bringing many practical new features. Below are 25 ES features that can significantly improve coding efficiency, making code cleaner, more elegant, and more performant.
1. Optional Chaining
Eliminate cumbersome null checks with the ?. operator for safe property access.
// Previous syntax
const street = user && user.address && user.address.street;
// New syntax
const street = user?.address?.street;2. Nullish Coalescing
Use the ?? operator to provide default values for null or undefined.
const value = null;
const defaultValue = value ?? 'default'; // 'default'3. Private Class Fields
Declare private fields with # to enhance encapsulation in OOP.
class Person {
#name;
constructor(name) {
this.#name = name;
}
getName() {
return this.#name;
}
}4. Dynamic Import
Load modules on demand to optimize application performance.
button.addEventListener('click', async () => {
const module = await import('./feature.js');
module.doSomething();
});5. Array.prototype.flat() and flatMap()
Easily handle nested arrays.
const nested = [1, [2, 3], [4, [5, 6]]];
const flattened = nested.flat(2); // [1, 2, 3, 4, 5, 6]6. Object Literal Enhancements
More concise property and method definitions.
const name = 'Tom';
const age = 18;
const person = {
name,
age,
sayHi() {
console.log('Hi!');
}
};7. Promise.allSettled()
Wait for all promises to settle, regardless of outcome.
const promises = [
fetch('/api/1'),
fetch('/api/2'),
fetch('/api/3')
];
const results = await Promise.allSettled(promises);8. BigInt
Handle integers larger than the safe Number range.
const bigNumber = 9007199254740991n;
const result = bigNumber + 1n;9. globalThis
A unified way to access the global object across environments.
console.log(globalThis);10. String.prototype.matchAll()
More powerful string matching capabilities.
const str = 'test1test2test3';
const regexp = /test(\d)/g;
const matches = [...str.matchAll(regexp)];11. Logical Assignment Operators
Simplify conditional assignments.
// Logical AND assignment
x &&= y; // equivalent to x && (x = y)
// Logical OR assignment
x ||= y; // equivalent to x || (x = y)
// Nullish coalescing assignment
x ??= y; // equivalent to x ?? (x = y)12. Promise.any()
Returns the first fulfilled promise.
const promises = [
fetch('/api/1'),
fetch('/api/2'),
fetch('/api/3')
];
try {
const first = await Promise.any(promises);
console.log(first);
} catch (error) {
console.log('All promises rejected');
}13. Numeric Separators
Improve readability of large numbers.
const billion = 1_000_000_000;
const bytes = 0xFF_FF_FF_FF;14. String.prototype.replaceAll()
Replace all occurrences of a substring.
15. WeakRef and FinalizationRegistry
Better memory‑management mechanisms.
16. Top‑Level Await
Use await directly in module scope.
17. Static Initialization Blocks
More flexible initialization of static class members.
18. Array.at()
Intuitive array index access, supporting negative indices.
19. Object.hasOwn()
Safer property existence checking.
20. Error Cause
Better error tracing with a cause property.
21. Object.groupBy()
Group array elements by a criterion.
22. RegExp Named Capture Groups
Clearer regex match results with named groups.
23. Promise.withResolvers()
More elegant control over Promise resolution.
24. Array Copy Methods
Convenient non‑mutating array operations.
const arr = [1, 2, 3];
const copy = arr.toReversed(); // does not modify original
const sorted = arr.toSorted(); // does not modify original25. Decorators
Enhance classes and members with reusable behavior.
function logged(target, context) {
return class extends target {
exec(...args) {
console.log('Starting execution...');
const result = super.exec(...args);
console.log('Finished execution.');
return result;
}
};
}
@logged
class Example {
exec() {
// ...
}
}Feel free to add more features.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
JavaScript
Provides JavaScript enthusiasts with tutorials and experience sharing on web front‑end technologies, including JavaScript, Node.js, Deno, Vue.js, React, Angular, HTML5, CSS3, and more.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
