Search code examples
javascriptdictionaryecmascript-6iso

Get country name from ISO code in Javascript


Having the list of country ISO codes, they are available here for reference:

Is there a way to return the country name from it?

Like, having a function, getCountryName(), which if called getCountryName('AL') will return 'Albania' and so on.

I was doing it as saving the whole list and work on it as with a dictionary but I was wondering if there is a method without saving the whole countries into a list.


Solution

  • You can try Intl.DisplayNames() of ECMAScript Internationalization API Intl:

    var getCountryNames = new Intl.DisplayNames(['en'], {type: 'region'});
    console.log(getCountryNames.of('AL'));  // "Albania"