
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
About the author
I will raise your revenues or decrease your costs using solutions based on logic and in most cases, it will involve a computer. Design - Build - Improve.
I design solutions to your business problem, build it with you and your team and finally scale the solution to fix other related problems.
I use Javascript and Python as the main languages for development but I am eager and faster to learn anything that would be put a solution on the table.
Comments
Join the discussionQuick Bits
This series include short articles and tutorials. The purpose of these articles is to help you quickly solve an issue or learn an easy concept.


