Full answer
Many everyday tasks now have built-in methods that older code did by hand.
Non-mutating array methods (ES2023): sort, reverse, and splice change the original array, which causes bugs in shared state. Their copying versions are toSorted(compare), toReversed(), and toSpliced(start, deleteCount, ...items), and with(index, value) returns a copy with one element replaced. The originals stay untouched. This fits React state and other immutable patterns.
Finding from the end (ES2023): findLast(predicate) and findLastIndex(predicate) search backwards. at(-1) (ES2022) reads from the end without length - 1.
Grouping (ES2024): Object.groupBy(items, callback) returns a plain object with null prototype, whose keys are the strings the callback returns and whose values are arrays. Map.groupBy does the same but allows any key type, including objects.
Set methods (ES2025): union, intersection, difference, symmetricDifference, isSubsetOf, isSupersetOf, and isDisjointFrom take another set (or a set-like object) and return a new Set or a boolean.
Other useful additions: Object.hasOwn(obj, key) replaces hasOwnProperty.call, Object.fromEntries is the reverse of Object.entries, Array.prototype.flat and flatMap, structuredClone, String.prototype.replaceAll, Promise.withResolvers() (which returns a promise with its resolve and reject functions), and Array.fromAsync.
Support: these features are in current versions of Node.js and the main browsers, but not in older ones. For a library or a site with old browser requirements, check a compatibility table, and use a polyfill or a build step if needed. Do not assume a feature exists because it exists on your machine.
In interviews, show that you reach for the non-mutating forms in state updates and the grouping helpers in place of manual reduce code.
const scores = [30, 5, 100, 12];
console.log(scores.toSorted((a, b) => a - b), scores.toReversed(), scores);
console.log(scores.toSpliced(1, 2, 'x'), scores.with(0, 99), scores);
console.log(scores.findLast((n) => n < 20), scores.findLastIndex((n) => n > 1000), scores.at(-1));
try {
scores.with(10, 1);
} catch (error) {
console.log('with out of range:', error.name);
}
const people = [
{ name: 'Asha', city: 'Pune' },
{ name: 'Ben', city: 'Oslo' },
{ name: 'Chen', city: 'Pune' },
];
const byCity = Object.groupBy(people, (person) => person.city);
console.log(byCity, Object.getPrototypeOf(byCity));
const byLength = Map.groupBy(people, (person) => (person.name.length > 3 ? 'long' : 'short'));
console.log(byLength);
const a = new Set([1, 2, 3]);
const b = new Set([3, 4]);
console.log(a.union(b), a.intersection(b), a.difference(b), a.symmetricDifference(b));
console.log(new Set([3]).isSubsetOf(a), a.isSupersetOf(new Set([1, 2])), a.isDisjointFrom(new Set([9])));
console.log(Object.hasOwn({ x: 1 }, 'x'), Object.fromEntries(new Map([['k', 1]])), 'a-b-c'.replaceAll('-', '+'));
const { promise, resolve } = Promise.withResolvers();
setTimeout(() => resolve('resolved from outside'), 1);
promise.then(console.log);
Array.fromAsync((async function* () { yield 1; yield 2; })()).then((values) => console.log('fromAsync:', values));Run with Node.js v24.22.1 as js-132.js; two runs gave identical output. Output: [ 5, 12, 30, 100 ] [ 12, 100, 5, 30 ] [ 30, 5, 100, 12 ] [ 30, 'x', 12 ] [ 99, 5, 100, 12 ] [ 30, 5, 100, 12 ] 12 -1 12 with out of range: RangeError [Object: null prototype] { Pune: [ { name: 'Asha', city: 'Pune' }, { name: 'Chen', city: 'Pune' } ], Oslo: [ { name: 'Ben', city: 'Oslo' } ] } null Map(2) { 'long' => [ { name: 'Asha', city: 'Pune' }, { name: 'Chen', city: 'Pune' } ], 'short' => [ { name: 'Ben', city: 'Oslo' } ] } Set(4) { 1, 2, 3, 4 } Set(1) { 3 } Set(2) { 1, 2 } Set(3) { 1, 2, 4 } true true true true { k: 1 } a+b+c fromAsync: [ 1, 2 ] resolved from outside
Key points
- toSorted, toReversed, toSpliced, and with return copies instead of changing the array.
- findLast, findLastIndex, and at read from the end.
- Object.groupBy and Map.groupBy group items by a key function.
- Set union, intersection, difference and related methods are built in now.
- Check runtime support; use polyfills or a build step for older targets.
Common mistakes
- Using sort or reverse on an array that belongs to shared state.
- Assuming a new method exists in every target environment.
- Writing manual reduce code for grouping when groupBy exists.
- Using with and expecting it to grow the array: an index out of range throws.
Follow-ups
- How would you polyfill toSorted for an older environment?
- Why does Object.groupBy return an object with no prototype?
- What is the difference between Object.groupBy and Map.groupBy?
Tips
- Group the methods by purpose, not as a long list.
- Mention support and polyfills, not only the methods.
- Tie the non-mutating methods to immutable state updates.