JavaScript 2025 cheatsheet
JavaScript 2025: Arrays
13 common JavaScript 2025 tasks.
Transform every item
Create a new array by applying a function to each item.
const doubled = numbers.map((n) => n * 2);
Keep matching items
Create a new array with the items that pass a test.
const adults = users.filter((u) => u.age >= 18);
Combine items into one value
Add up a property across an array. The second argument is the starting value.
const total = items.reduce((sum, item) => sum + item.price, 0);
Find the first match
Return the first item that passes a test, or undefined if none does.
const user = users.find((u) => u.id === 42);
Test items against a condition
Check whether any item, or every item, passes a test.
const hasAdmin = users.some((u) => u.role === "admin");
const allActive = users.every((u) => u.active);
Check if an array contains a value
Return true if the value is in the array.
const found = [1, 2, 3].includes(2);
Map and flatten in one step
Collect the tags of every post into a single array.
const tags = posts.flatMap((post) => post.tags);
Sort numbers without changing the original
Sort ascending. sort() compares as text by default and changes the array, so toSorted with a comparator is safer.
const sorted = numbers.toSorted((a, b) => a - b);
Create a range of numbers
Build an array of the numbers 0 to 4.
const range = Array.from({ length: 5 }, (_, i) => i);
Group items by a property
Return an object whose keys are the group names. Available in current browsers and Node.js 21 and later.
const byRole = Object.groupBy(users, (user) => user.role);