Quiz

What is the `Intl` namespace object for?

Topics
JavaScriptInternationalization

TL;DR

Intl provides locale-aware formatting and comparison for numbers, currencies, dates, times, lists, relative time, plural categories, segments, and more. Pass the locale and domain options explicitly rather than hand-building localized strings. For dates, specify the intended time zone; for money, store the currency separately from the numeric amount. Exact output can vary with locale data, so test semantic parts rather than brittle punctuation where possible.

const date = new Date('2026-08-11T04:00:00Z');
const formatter = new Intl.DateTimeFormat('en-US');
console.log(formatter.format(date)); // Locale-formatted calendar date

What is the Intl namespace object for?

The Intl namespace object in JavaScript is a part of the ECMAScript Internationalization API, which provides language-sensitive string comparison, number formatting, and date and time formatting. This is particularly useful for applications that need to support multiple languages and regions.

Language-sensitive string comparison

The Intl.Collator object is used for comparing strings in a locale-aware manner. This is useful for sorting strings in a way that is consistent with the conventions of a particular language.

const collator = new Intl.Collator('de-DE');
console.log(collator.compare('ä', 'z')); // Outputs a negative number because 'ä' comes before 'z' in German

Number formatting

The Intl.NumberFormat object is used for formatting numbers according to the conventions of a specific locale. This includes formatting for currency, percentages, and plain numbers.

const number = 1234567.89;
const formatter = new Intl.NumberFormat('de-DE', {
style: 'currency',
currency: 'EUR',
});
console.log(formatter.format(number)); // German-formatted EUR amount

Date and time formatting

The Intl.DateTimeFormat object is used for formatting dates and times according to the conventions of a specific locale.

const date = new Date();
const formatter = new Intl.DateTimeFormat('en-GB', {
year: 'numeric',
month: 'long',
day: 'numeric',
});
console.log(formatter.format(date)); // British English long date

Plural rules

The Intl.PluralRules object is used to get the plural form of a number in a specific locale. This is useful for correctly pluralizing words in different languages.

const pluralRules = new Intl.PluralRules('en-US');
console.log(pluralRules.select(1)); // Outputs 'one'
console.log(pluralRules.select(2)); // Outputs 'other'

Intl.PluralRules returns a category; the application still provides translated message forms. English-style count === 1 ? singular : plural logic does not generalize to every language.

Practical considerations

const formatter = new Intl.DateTimeFormat('en-GB', {
dateStyle: 'medium',
timeStyle: 'short',
timeZone: 'Asia/Singapore',
});
console.log(formatter.format(new Date('2026-08-11T04:00:00Z')));
  • Reuse a formatter when formatting many values instead of constructing one for every table cell.
  • Use formatToParts() when markup must wrap specific semantic pieces; do not split a formatted string on assumed punctuation.
  • Locale fallback and supported options depend on the runtime's locale data. Use supportedLocalesOf() when exact locale support matters.
  • Intl formats values but does not translate arbitrary product copy or convert time zones into business-calendar rules.

Further reading

Exercícios

Verifique seu entendimento
Beta
Verifique seu entendimento Exercício 1 de 4
Verifique seu entendimento Exercício 1 de 4

A German-speaking customer views a USD invoice and a meeting held at a fixed Singapore time. Which choices preserve those domain meanings while localizing presentation? Select all that apply.