Skip to content

.tip

ยท one of the small things

Intl formats numbers and dates, so do not hand-roll it

Every hand-written thousands separator or relative time string is wrong for some locale, and the platform already has formatters for all of it.

new Intl.NumberFormat("en-GB", { style: "currency", currency: "GBP" }).format(1234.5);
new Intl.RelativeTimeFormat("en", { numeric: "auto" }).format(-1, "day"); // "yesterday"
new Intl.DateTimeFormat("de-DE", { dateStyle: "medium" }).format(date);

Create each formatter once and reuse it, since constructing it is the expensive part.

javascripti18n