JavaScript Intl.PluralRules: Localized Grammatical Categories
Quick answer
Intl.PluralRules selects a grammatical plural category for a numeric value. Languages do not share one universal singular/plural rule.
How it works
Construct new Intl.PluralRules(locale, {type: "cardinal"}) for counting, or type: "ordinal" for ranks. select(number) returns a category such as one, few, many or other. The available categories depend on locale and options. Rounding options influence selection. The category selects a message template; it is not a translated noun or complete sentence.
const p=new Intl.PluralRules("en",{type:"cardinal"}); console.log(p.select(1),p.select(0),p.select(2));Expected output in Node.js:
one other otherEnglish cardinal categories use one for 1 and other for 0 and 2.
const p=new Intl.PluralRules("en",{type:"ordinal"}); console.log(p.select(21),p.select(22),p.select(23),p.select(11));Expected output in Node.js:
one two few otherEnglish ordinal categories distinguish suffix patterns, including the 11 exception.
Common mistakes
Do not hardcode n === 1 as the universal grammatical rule; maintain a fallback for other and use translated templates.
Practice
Return the English cardinal category for an integer count; compare it with the ordinal category.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
JavaScript Intl.PluralRules: Localized Grammatical Categories — Knowledge Check
Apply the taught concept and predict the output.
What does Intl.PluralRules.select return?
Which type is appropriate for ranks like 21st?
Can every language use the same two plural message forms?
What is the exact console output in JavaScript Intl.PluralRules: Localized Grammatical Categories example 1?
const p=new Intl.PluralRules("en",{type:"cardinal"}); console.log(p.select(1),p.select(0),p.select(2));What is the exact console output in JavaScript Intl.PluralRules: Localized Grammatical Categories example 2?
const p=new Intl.PluralRules("en",{type:"ordinal"}); console.log(p.select(21),p.select(22),p.select(23),p.select(11));