Modern JavaScript Features You Should Be Using (ES2020 to ES2025)
JavaScript gets a new edition every year. If you learned it a while ago, these additions will make your code shorter and safer.
Safer property access
const city = user?.address?.city; // undefined instead of a crash
const limit = options.limit ?? 20; // only falls back on null/undefined, not 0
settings.theme ??= 'light'; // assign if missing
Arrays
const last = items.at(-1); // last element
const sorted = prices.toSorted((a, b) => a - b); // returns a copy; prices unchanged
const updated = todos.with(2, { ...todos[2], done: true });
const lastError = logs.findLast((l) => l.level === 'error');
const byStatus = Object.groupBy(orders, (o) => o.status);
Objects and copying
const copy = structuredClone(state); // deep copy, handles Dates, Maps, cycles
Object.hasOwn(obj, 'id'); // safer than obj.hasOwnProperty('id')
Async helpers
const results = await Promise.allSettled(requests); // never rejects; inspect each result
const fastest = await Promise.any(mirrors.map(fetch));
const { promise, resolve, reject } = Promise.withResolvers();
const value = await Promise.try(() => mightThrowOrReturnPromise());
ES2025 additions
// Set methods
const both = admins.intersection(editors);
const all = admins.union(editors);
// Iterator helpers: lazy map/filter/take on any iterator
const firstTen = numbers().filter((n) => n % 2 === 0).take(10).toArray();
// Escape user input for a RegExp
const re = new RegExp(RegExp.escape(searchTerm), 'i');
// JSON modules
import config from './config.json' with { type: 'json' };
Other gems
- Numeric separators:
1_000_000. String.prototype.replaceAll.- Top-level
awaitin modules. - Private class fields:
#balance. Array.fromAsyncto collect async iterables.
Check support for your target browsers (MDN's compatibility tables help); the newest features may need a recent browser or Node.js version.
Was this article helpful?
Comments
No comments yet. Have a question or a tip? Be the first to share it.