Country Code to Flag Emoji

Instead of showing boring countrycodes like US, CH, NL, it is much nicer to show the flag emojis, ?? ?? and ??, right? This shouldn’t be hard to do with some JavaScript.

function getFlagEmoji(countryCode) {
const codePoints = countryCode
.toUp…


This content originally appeared on DEV Community and was authored by Jorik

Instead of showing boring countrycodes like US, CH, NL, it is much nicer to show the flag emojis, ?? ?? and ??, right? This shouldn't be hard to do with some JavaScript.

function getFlagEmoji(countryCode) {
  const codePoints = countryCode
    .toUpperCase()
    .split('')
    .map(char =>  0x1F1E6 + (char.charCodeAt(0) - 65));
  return String.fromCodePoint(...codePoints);
}

The Flag emoji is a combination of the two characters, starting at unicode position 0x1F1E6 for the letter A. For CH (Switzerland), we want the output to be 0x1F1E9 0x1F1ED. These are hexadecimal numbers, which we can just compute with like decimal numbers.

Run down of what happens in this small little function;

  • First, uppercase the country code input.
  • Split into an array, to iterate over each character.
  • Receive the UTF-16 index from the character using charCodeAt(). For the letter A, this is 65. We should substract the starting point (65) from this number to get the index of the character in the alphabet. Now, simply add this to the flag starting index (0x1F1E6).
  • The String.fromCodePoint function will return the character, which will be the flag emoji when we provide both the characters.

Example:

getFlagEmoji('US') = '??'
getFlagEmoji('NL') = '??'
getFlagEmoji('CH') = '??'

Instant flags with no load time or external resources!


This content originally appeared on DEV Community and was authored by Jorik


Print Share Comment Cite Upload Translate Updates
APA

Jorik | Sciencx (2021-04-09T11:03:21+00:00) Country Code to Flag Emoji. Retrieved from https://www.scien.cx/2021/04/09/country-code-to-flag-emoji/

MLA
" » Country Code to Flag Emoji." Jorik | Sciencx - Friday April 9, 2021, https://www.scien.cx/2021/04/09/country-code-to-flag-emoji/
HARVARD
Jorik | Sciencx Friday April 9, 2021 » Country Code to Flag Emoji., viewed ,<https://www.scien.cx/2021/04/09/country-code-to-flag-emoji/>
VANCOUVER
Jorik | Sciencx - » Country Code to Flag Emoji. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/04/09/country-code-to-flag-emoji/
CHICAGO
" » Country Code to Flag Emoji." Jorik | Sciencx - Accessed . https://www.scien.cx/2021/04/09/country-code-to-flag-emoji/
IEEE
" » Country Code to Flag Emoji." Jorik | Sciencx [Online]. Available: https://www.scien.cx/2021/04/09/country-code-to-flag-emoji/. [Accessed: ]
rf:citation
» Country Code to Flag Emoji | Jorik | Sciencx | https://www.scien.cx/2021/04/09/country-code-to-flag-emoji/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.