CKTIS / JavaScript
Intl.PluralRules.prototype.select(): choose a plural category
`select(value)` returns a grammatical category for one value using the instance’s locale and options. Use the result to choose a translated message, not to format a number.
Version: ECMAScript 2024 and the current web platform; runtime support varies
Syntax
rules.select(value)Intl.PluralRules.prototype.select is an instance method. Create rules with new Intl.PluralRules(locales, options), then call rules.select(value). It returns one of the strings "zero", "one", "two", "few", "many", or "other". The available categories depend on the resolved locale and rule type; "one" is not a universal test for value === 1. Use the category as a key into your translated message variants: the method neither translates text nor formats a number.
The default "cardinal" type answers “how many?”; "ordinal" answers “which position?”. Rounding and digit options such as minimumFractionDigits can change the category because rules can depend on visible fraction digits. select classifies one value; selectRange classifies a whole range. Check rules.resolvedOptions().locale for the locale actually used, which may differ from the one requested.
The receiver (this) must be an initialized Intl.PluralRules instance. Calling the method on the prototype itself or calling it as a detached, unbound function throws TypeError. In both ECMA-402 (2024) and the living draft, select uses ToNumber: numeric strings are converted to Number values ("2" becomes 2), while BigInt and Symbol inputs throw TypeError. An omitted value becomes NaN; NaN and both infinities return "other". A returned category therefore does not validate the input.
Examples
{
const cardinal = new Intl.PluralRules('en-US');
const ordinal = new Intl.PluralRules('en-US', { type: 'ordinal' });
const withFraction = new Intl.PluralRules('en-US', { minimumFractionDigits: 1 });
console.log(cardinal.select(1));
console.log(cardinal.select(2));
console.log(ordinal.select(2));
console.log(withFraction.select(1));
}Result
one other two other
With US English rules, `2` is `other` as a cardinal but `two` as an ordinal. Requiring one fraction digit makes `1` classify as `1.0`, so it returns `other` instead of `one`.
Related lessons
- JavaScript · Lesson 24 · Advanced · Version: 4
Sources
- ECMAScript® 2024 Internationalization API Specification — Intl.PluralRules.prototype.select
- ECMAScript® 2024 Internationalization API Specification — ResolvePlural
- ECMAScript® 2024 Language Specification — ToNumber
- ECMA-402 living draft — Intl.PluralRules.prototype.select
- MDN — Intl.PluralRules.prototype.select()
- MDN — Intl.PluralRules() constructor