Intl.NumberFormat, dates & localeCompare
Format for people
Lesson 18 of 20 · 10 min · Module 5: Real-world data
1234.5 is "$1,234.50" for an American, "1.234,50 €" for a German, and names like "Émile" should sort next to "Emma", not after "Zoe". The built-in Intl API handles these rules for every language.
Start this lesson →Free. No installation: you write and run the code in your browser, and it is checked automatically.
By the end, you’ll be able to…
- Format currency for different countries
- Compare text the way people read it
- Format dates without time-zone shifts
The idea
new Intl.NumberFormat(locale, { style: "currency", currency }) formats money. Intl.DateTimeFormat formats dates. a.localeCompare(b, locale, { sensitivity: "base" }) compares text the way people expect, ignoring case and accents.
const eur = new Intl.NumberFormat("de-DE", { style: "currency", currency: "EUR" });
console.log(eur.format(1234.5));
console.log(["b", "a", "C"].sort((x, y) => x.localeCompare(y)));
01Locales carry rules
The locale decides the decimal separator, the grouping symbol, where the currency sign goes, and even the spaces used. Building such strings by hand is error-prone for every country but your own.
02Default sorting is by code points
["b", "a", "C"].sort() gives ["C", "a", "b"] because uppercase letters have lower codes. localeCompare with sensitivity "base" treats a, A and á as equal letters.
console.log(["b", "a", "C"].sort());
console.log(["b", "a", "C"].sort((x, y) => x.localeCompare(y)));["C","a","b"]
["a","b","C"]03Pin the time zone
Formatting midnight UTC in a time zone west of UTC shows the previous day. timeZone: "UTC" formats the calendar date exactly as stored.
Your turn
formatPrice(cents, currency = "USD", locale = "en-US") that formats an amount given in cents as currency. Write sortNames(names, locale = "en") that returns a new, alphabetically sorted array, ignoring case and accents, without changing the input. Write formatDate(iso, locale = "en-GB") that turns "2026-10-20" into "20 October 2026" (day numeric, month long, year numeric, in UTC).What the checks look for
- Formats US dollars
- Formats euros for German readers
- Sorts names ignoring case and accents, without changing the input
- Formats a date in words
Take this with you
Let Intl handle language rules instead of hand-made formatting.
Go a little further
Use Intl.RelativeTimeFormat to show "in 3 days" or "2 days ago" for a deadline.