Generating Flag Emojis from ISO Country Codes

Learn how to programmatically generate flag emojis from ISO 3166-1 alpha-2 country codes using Unicode Regional Indicator Symbols in JavaScript and other languages.

Programming

Detailed Explanation

How Flag Emojis Work

Flag emojis are not stored as single characters. They are composed of two Regional Indicator Symbols that correspond to the letters of an ISO 3166-1 alpha-2 country code.

The Unicode Mechanism

Unicode defines Regional Indicator Symbols in the range U+1F1E6 (A) to U+1F1FF (Z). Each symbol represents a letter:

Letter Code Point Symbol
A U+1F1E6 Regional Indicator A
B U+1F1E7 Regional Indicator B
... ... ...
Z U+1F1FF Regional Indicator Z

When two regional indicator symbols appear together and form a valid ISO 3166-1 alpha-2 code, the system renders a flag. For example:

  • US = U+1F1FA (S→U) + U+1F1F8 (S) = flag emoji for United States
  • JP = U+1F1EF (J) + U+1F1F5 (P) = flag emoji for Japan
  • GB = U+1F1EC (G) + U+1F1E7 (B) = flag emoji for United Kingdom

JavaScript Implementation

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

console.log(getFlagEmoji('US')); // flag for US
console.log(getFlagEmoji('JP')); // flag for Japan
console.log(getFlagEmoji('BR')); // flag for Brazil

Python Implementation

def get_flag_emoji(country_code: str) -> str:
    return ''.join(
        chr(0x1F1E6 + ord(c) - ord('A'))
        for c in country_code.upper()
    )

print(get_flag_emoji('US'))  # flag for US
print(get_flag_emoji('DE'))  # flag for Germany

Platform Differences

Flag rendering depends on the platform and font:

Platform Behavior
iOS / macOS Renders as colored flag images
Android Renders as colored flag images
Windows Shows letters instead of flags (by design)
Linux Depends on the font (Noto Color Emoji supports flags)

Edge Cases

  • Invalid codes (e.g., "XX") render as two letter-like symbols, not flags
  • Windows intentionally does not render flag emojis (shows regional indicator letters instead)
  • Subdivision flags (e.g., Scotland, Texas) use tag sequences, not regional indicators

Use Case

A country selector component in a React app displays flag emojis next to each country name. The component derives the flag from the alpha-2 code without needing to ship flag image assets, reducing bundle size by hundreds of kilobytes.

Try It — Country Code Reference

Open full tool