Skip to main content

How to Get a Country Name from a Country Code in JavaScript

When working with international data, you often need to convert standardized codes—like a country code ("US"), a language code ("en-GB"), or a currency code ("EUR")—into a full, human-readable name. The modern, built-in JavaScript API for this is the Intl.DisplayNames object.

This guide will teach you how to use this powerful internationalization tool to get the display names for regions (countries), languages, and currencies in any supported language.

The Core Method: Intl.DisplayNames

The Intl.DisplayNames constructor is the standard way to create an object that can translate various codes into display names. It is highly flexible and handles localization for you.

The logic:

  1. Create an instance: Create a new Intl.DisplayNames object. You provide two arguments:
    • A locale (e.g., 'en' for English, 'es' for Spanish). This determines the language of the output string.
    • An options object where you specify the type of code you want to translate (e.g., 'region', 'language', or 'currency').
  2. Get the name: Call the .of() method on the created object, passing it the code you want to look up.

Use Case 1: Get a Country Name from a Region Code

This is the most common use case. The type: 'region' option is used for ISO 3166 country codes.

We have a country code like "US" and need to display its full name, "United States".

// Problem: How to get the full country name for this code?
const countryCode = 'US';

Solution:

// 1. Create an instance for the English locale and region type.
const regionNames = new Intl.DisplayNames(['en'], { type: 'region' });

// 2. Get the name by passing the code to the .of() method.
console.log(regionNames.of('US')); // Output: "United States"
console.log(regionNames.of('DE')); // Output: "Germany"
console.log(regionNames.of('JP')); // Output: "Japan"

To get the names in a different language, simply change the locale:

const regionNamesInGerman = new Intl.DisplayNames(['de'], { type: 'region' });

console.log(regionNamesInGerman.of('US')); // Output: "Vereinigte Staaten"

Use Case 2: Get a Language Name from a Language Code

By changing the type to 'language', you can get the full name of a language from its BCP 47 language tag.

Problem: we have a language code like "en-GB" and need to display its full name, "British English".

Solution:

const languageNames = new Intl.DisplayNames(['en'], { type: 'language' });

console.log(languageNames.of('en-US')); // Output: "American English"
console.log(languageNames.of('fr')); // Output: "French"
console.log(languageNames.of('zh-Hant')); // Output: "Traditional Chinese"

Use Case 3: Get a Currency Name from a Currency Code

Similarly, setting type: 'currency' allows you to translate ISO 4217 currency codes.

Problem: we have a currency code like "JPY" and need to display its full name, "Japanese Yen".

Solution:

const currencyNames = new Intl.DisplayNames(['en'], { type: 'currency' });

console.log(currencyNames.of('USD')); // Output: "US Dollar"
console.log(currencyNames.of('EUR')); // Output: "Euro"
console.log(currencyNames.of('JPY')); // Output: "Japanese Yen"

Conclusion

The Intl.DisplayNames API is the modern, standard, and most powerful tool for converting standardized codes into human-readable names. It removes the need for third-party libraries or large, manual lookup objects for this common internationalization task.

  • The core process is to create an Intl.DisplayNames instance with a specific locale and type.
  • The type option determines what you are translating:
    • 'region': For country codes.
    • 'language': For language codes.
    • 'currency': For currency codes.
  • The .of(code) method performs the lookup and returns the translated string.