Code Reference

Intl

JavaScript · Reference cheat sheet

Intl

JavaScript · Reference cheat sheet


📋 Overview

The Intl namespace provides locale-aware formatting and comparison: dates, numbers, lists, relative time, plurals, and collation. Prefer Intl over hand-rolled locale strings for i18n correctness and accessibility.

🔧 Core concepts

ConstructorUse
Intl.DateTimeFormatdates/times
Intl.NumberFormatnumbers/currency/units
Intl.RelativeTimeFormat“3 days ago”
Intl.ListFormat“A, B, and C”
Intl.PluralRulesplural categories
Intl.Collatorsort / compare strings
Intl.DisplayNameslanguage/region names
Intl.Segmentergrapheme/word segments
  • Locales: BCP 47 tags — "en", "en-US", "de-DE", navigator.language.
  • Options: style, currency, timeZone, numberingSystem, etc.
  • supportedLocalesOf: feature detection for locale lists.
new Intl.NumberFormat("en-US", {
  style: "currency",
  currency: "USD",
}).format(1234.5); // "$1,234.50"

💡 Examples

const locale = navigator.language || "en";

// Dates
new Intl.DateTimeFormat(locale, {
  dateStyle: "medium",
  timeStyle: "short",
}).format(new Date());

// Time zone
new Intl.DateTimeFormat("en-GB", {
  timeZone: "UTC",
  timeStyle: "long",
}).format(new Date());

// Relative
new Intl.RelativeTimeFormat(locale, { numeric: "auto" }).format(-1, "day");
// "yesterday" (en)

// Lists
new Intl.ListFormat(locale, { style: "long", type: "conjunction" }).format([
  "red",
  "green",
  "blue",
]);

// Sort
const collator = new Intl.Collator(locale, { sensitivity: "base" });
["ä", "a", "z"].sort(collator.compare);

// Plurals
const pr = new Intl.PluralRules("ru");
pr.select(3); // "few" etc.

// Display names
new Intl.DisplayNames("en", { type: "language" }).of("fr"); // "French"
// Compact notation
new Intl.NumberFormat("en", {
  notation: "compact",
  compactDisplay: "short",
}).format(1_200_000); // "1.2M"

⚠️ Pitfalls

  • Locale data availability varies by engine — test critical locales.
  • Don’t concatenate translated fragments — format whole messages (use ICU MessageFormat libs for complex cases).
  • Date#toLocaleString is convenient but less reusable than a shared DateTimeFormat instance.
  • Currency requires ISO codes (USD), not symbols alone.
  • Sorting without Collator breaks non-English languages.

On this page