Get Country Flag using ISO Country Code

Get Country Flag using ISO Country Code

Wouldn’t it be nice to be able to easily convert a regular ISO 3166-1 alpha-2 country code to its respective Unicode emoji flag? Here we go...

'GB'.toUpperCase().replace(/./g, (char) => String.fromCodePoint(char.charCodeAt(0) + 127397));

This will turn “GB” into 🇬🇧 (if it displays 🇬​🇧 instead of the British flag, your system does not support emoji flags yet).

You can now write a reusable function that every time you pass a country ISO Code, you get an emoji flag.

const countryToFlag = (isoCode) =>
    isoCode
        .toUpperCase()
        .replace(/./g, (char) => String.fromCodePoint(char.charCodeAt(0) + 127397));

To convert from an emoji flag to the country code:

'🇩🇪.replace(/../g, cp => String.fromCharCode(cp.codePointAt(0)-127397) );

NOTE: This is not supported in IE 11

 
Share this