Use the Intl.DisplayNames() constructor to get a country name from a country code.
The of() method on the returned object takes a code as a parameter and returns a string based on the provided locale.
const regionNames = new Intl.DisplayNames(
['en'], {type: 'region'}
);
console.log(regionNames.of('US')); // 👉️ "United States"
console.log(regionNames.of('GB')); // 👉️ "United kingdom"
console.log(regionNames.of('DE')); // 👉️ "Germany"
console.log(regionNames.of('AU')); // 👉️ "Australia"
The Intl.DisplayNames() constructor returns an object we can use to:
- translate a region code to the country’s name.
- translate a language code to the language’s name.
- translate a currency code to the currency’s display name.