0 day streak
javascript / intermediate
Snippet

Internationalized List Formatting

The Intl.ListFormat object enables language-sensitive list formatting, automatically handling separators and conjunctions like 'and' or 'or' based on the locale.

snippet.js
javascript
1
2
3
4
5
6
7
8
9
10
11
12
13
14
const items = ['HTML', 'CSS', 'JavaScript'];
const enFormatter = new Intl.ListFormat('en', {
style: 'long',
type: 'conjunction'
});
const deFormatter = new Intl.ListFormat('de', {
style: 'short',
type: 'disjunction'
});
console.log(enFormatter.format(items)); // "HTML, CSS, and JavaScript"
console.log(deFormatter.format(items)); // "HTML, CSS oder JavaScript"
Breakdown
1
new Intl.ListFormat('en', ...)
Initializes a formatter for the English language with specific styling.
2
type: 'disjunction'
Specifies that the list items are alternatives (using 'or' instead of 'and').