Intl.ListFormat
Joins a list into a phrase with commas and a conjunction: a, b, and c.
Available since: ES2021
Syntax
new Intl.ListFormat(locales, options)Parameters
| Parameter | Type | Description |
|---|---|---|
| locales | string | Optional. The language. |
| options | object | Optional. type: conjunction (and), disjunction (or), unit; style: long, short, narrow. |
Return value
Intl.ListFormatAn object with a format(list) method.
Examples
A list with and and with or:
const names = ['Anna', 'Oleg', 'Ivan'];
console.log(new Intl.ListFormat('en', { type: 'conjunction' }).format(names));
console.log(new Intl.ListFormat('en', { type: 'disjunction' }).format(names));Output:
Anna, Oleg, and Ivan
Anna, Oleg, or IvanOther languages:
const names = ['Anna', 'Oleg', 'Ivan'];
console.log(new Intl.ListFormat('de').format(names));
console.log(new Intl.ListFormat('ru').format(names));Output:
Anna, Oleg und Ivan
Anna, Oleg и IvanNotes
Unlike join(), the formatter puts a conjunction before the last item and knows the language rules — such as the comma before and in US English.
See also
- Array.prototype.join — Joins array elements into a string.
- Intl.PluralRules — Picks the word form for a number: 1 file, 2 files.
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.