category | created | tags | title |
---|---|---|---|
Tip |
2021-02-27 |
JavaScript |
Format a list |
We can take the advantage of the Intl.ListFormat object to format a list for a given locale:
const people = ['Foo', 'Bar', 'Fuzz'];
new Intl.ListFormat('en', { type: 'conjunction' }).format(people);
// 'Foo, Bar, and Fuzz'
new Intl.ListFormat('en-GB', { type: 'disjunction' }).format(people);
// 'Foo, Bar, or Fuzz'