JavaScript 2025 cheatsheet
JavaScript 2025: Strings
9 common JavaScript 2025 tasks.
Insert values into a string
Use backticks and ${} to build strings, including multi-line ones.
const message = `Hello, ${user.name}! You have ${count} new messages.`;
Split and join
Turn a string into an array, and back again.
const words = "a,b,c".split(",");
const line = words.join(" | ");
Replace every occurrence
Replace all matches, not just the first.
const clean = text.replaceAll("-", " ");
Pad a string
Pad on the left, for example to make 7 into 07.
const hour = String(7).padStart(2, "0");
Check the start, end or contents
Test a string without a regular expression.
url.startsWith("https://");
file.endsWith(".png");
text.includes("error");
Capitalize a word
Uppercase the first letter and keep the rest.
const title = word[0].toUpperCase() + word.slice(1);
Sort text correctly
Sort strings using the language's rules, including accents and capitals.
names.sort((a, b) => a.localeCompare(b));
Format numbers and currency
Format with the user's locale conventions.
new Intl.NumberFormat("en-US", { style: "currency", currency: "USD" }).format(1234.5);
Format a date
Format a date for a locale.
new Intl.DateTimeFormat("en-GB", { dateStyle: "medium" }).format(new Date());