Course contentsShow
← JavaScript
Lesson 2230 of 2,72346. InternationalizationFree lesson

The select() Method

Use select() to determine which plural category (zero, one, two, few, many, other) a number belongs to in a locale.

The select() Method

You'll learn how to use select() to automatically determine which plural category a number belongs to for any locale.

What select() Does

The select() method is the workhorse of Intl.PluralRules. When you pass it a number, it returns a string indicating the plural category that number falls into for your locale: "zero", "one", "two", "few", "many", or "other".

Think of select() as a linguistic classifier. Just like a librarian categorizes books into genres, select() categorizes numbers into the grammatical plural forms that language requires. In English, we mostly use "one" (1 apple) and "other" (0, 2, 5 apples). But Polish has different rules for numbers ending in 2-4, and Arabic has six distinct categories!

How It Works

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

const plRules = new Intl.PluralRules('pl-PL');
console.log(plRules.select(1));   // "one"
console.log(plRules.select(2));   // "few" (Polish: 2-4 have special form)
console.log(plRules.select(5));   // "many"
console.log(plRules.select(1.5)); // "other" (decimals)

You typically use the returned category as a key to pick the correct translation:

const messages = {
  one: "1 message",
  other: "{count} messages"
};

const rules = new Intl.PluralRules('en-US');
const count = 5;
const category = rules.select(count);
console.log(messages[category].replace('{count}', count)); // "5 messages"

Key Takeaway: The select() method takes a number and returns its plural category string for the locale, letting you choose grammatically correct text without hardcoding language rules.