Encodera

Thanks for reading!

Donate to the site — we work hard for you.

JavaScript reference

Intl.PluralRules

Picks the word form for a number: 1 file, 2 files.

Available since: ES2018

Syntax

new Intl.PluralRules(locales, options)

Parameters

ParameterTypeDescription
localesstringOptional. The language.
optionsobjectOptional. type: cardinal (a count) or ordinal (a position).

Return value

Intl.PluralRulesAn object with a select(number) method.

Examples

Forms for English:

const rules = new Intl.PluralRules('en');
const words = { one: 'file', other: 'files' };
 
for (const n of [1, 2, 5]) {
  console.log(`${n} ${words[rules.select(n)]}`);
}

Output:

1 file
2 files
5 files

Russian has more forms:

const rules = new Intl.PluralRules('ru');
 
console.log(rules.select(1), rules.select(3), rules.select(5), rules.select(21));

Output:

one few many one

Ordinal numbers:

const rules = new Intl.PluralRules('en', { type: 'ordinal' });
const suffixes = { one: 'st', two: 'nd', few: 'rd', other: 'th' };
 
console.log([1, 2, 3, 4, 11, 22].map((n) => n + suffixes[rules.select(n)]).join(' '));

Output:

1st 2nd 3rd 4th 11th 22nd

Notes

The select() method returns a category, not a word: zero, one, two, few, many or other. You provide the words for each category yourself.

See also

In the tutorial

  • Formatting with Intl — Locale-aware formatting of numbers, money, dates and plural words in JavaScript: toLocaleString, Intl.NumberFormat, Intl.DateTimeFormat and Intl.PluralRules.

Thanks for reading!

Donate to the site — we work hard for you.