Get Country name from Country code in JavaScript

avatar
Borislav Hadzhiev

Last updated: Mar 6, 2024
2 min

banner

# Get Country name from Country code using JavaScript

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.

index.js
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"

get country name from country code

The code for this article is available on GitHub

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
The first parameter the Intl.DisplayNames() constructor takes is a string containing a BCP 47 language tag or an array of such strings.

The second parameter is an options object.

You can read about all the properties that are available on the options object in this section of the MDN docs.

The Intl.DisplayNames.of() method takes a country code as a parameter and returns a string based on the locale and the provided parameters when instantiating the Intl.DisplayNames object.

# Getting Country names in a different language

If you need the country names in a different language, change the locale when creating the object.

index.js
const regionNames = new Intl.DisplayNames(['de'], { type: 'region', }); console.log(regionNames.of('US')); // πŸ‘‰οΈ "Vereinigte Staaten" console.log(regionNames.of('DE')); // πŸ‘‰οΈ "Deutschland" console.log(regionNames.of('KR')); // πŸ‘‰οΈ "SΓΌdkorea" console.log(regionNames.of('CA')); // πŸ‘‰οΈ "Kanada"

getting country names in different language

The code for this article is available on GitHub

# Get a language name from a language code

Here is an example that shows how to get a language name from a language code.

index.js
const regionNames = new Intl.DisplayNames(['en'], { type: 'language', }); console.log(regionNames.of('en-US')); // πŸ‘‰οΈ "American English" console.log(regionNames.of('en-GB')); // πŸ‘‰οΈ "British English" console.log(regionNames.of('de-DE')); // πŸ‘‰οΈ "German (Germany)"

get language name from language code

The code for this article is available on GitHub

We changed the value of the type property in the options object and got the display names for the language codes.

# Convert a currency code to the currency's display name

You can also use this approach to convert a currency code to the currency's display name for the specific locale.

index.js
const regionNames = new Intl.DisplayNames(['en'], { type: 'currency', }); console.log(regionNames.of('USD')); // πŸ‘‰οΈ "US Dollar" console.log(regionNames.of('EUR')); // πŸ‘‰οΈ "Euro" console.log(regionNames.of('JPY')); // πŸ‘‰οΈ "Japanese Yen"

convert currency code to currency display name

The code for this article is available on GitHub

We set the type property to currency and got the display names of the currencies based on the provided locale.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.