Get emoji flag by country code
const input = document.getElementById("input");
const btn = document.getElementById("btn");
function getFlagEmoji(countryCode) {
const codePoints = countryCode.toUpperCase().split("").map((char) => 127397 + char.charCodeAt(0));
return String.fromCodePoint(...codePoints);
}
btn.addEventListener("click", e => {
e.preventDefault();
console.log(getFlagEmoji(input.value));
});
<input type="text" id="input" placeholder="Country Code" maxlength="2" />
<button id="btn">Get Emoji</button>
function getFlags($code){
$code = strtoupper($code);
if($code == 'AD') return 'ð¦ð©';
if($code == 'AE') return 'ð¦ðª';
if($code == 'AF') return 'ð¦ð«';
if($code == 'AG') return 'ð¦ð¬';
if($code == 'AI') return 'ð¦ð®';
if($code == 'AL') return 'ð¦ð±';
if($code == 'AM') return 'ð¦ð²';
if($code == 'AO') return 'ð¦ð´';
if($code == 'AQ') return 'ð¦ð¶';
if($code == 'AR') return 'ð¦ð·';
if($code == 'AS') return 'ð¦ð¸';
if($code == 'AT') return 'ð¦ð¹';
if($code == 'AU') return 'ð¦ðº';
if($code == 'AW') return 'ð¦ð¼';
if($code == 'AX') return 'ð¦ð½';
if($code == 'AZ') return 'ð¦ð¿';
if($code == 'BA') return 'ð§ð¦';
if($code == 'BB') return 'ð§ð§';
if($code == 'BD') return 'ð§ð©';
if($code == 'BE') return 'ð§ðª';
if($code == 'BF') return 'ð§ð«';
if($code == 'BG') return 'ð§ð¬';
if($code == 'BH') return 'ð§ð';
if($code == 'BI') return 'ð§ð®';
if($code == 'BJ') return 'ð§ð¯';
if($code == 'BL') return 'ð§ð±';
if($code == 'BM') return 'ð§ð²';
if($code == 'BN') return 'ð§ð³';
if($code == 'BO') return 'ð§ð´';
if($code == 'BQ') return 'ð§ð¶';
if($code == 'BR') return 'ð§ð·';
if($code == 'BS') return 'ð§ð¸';
if($code == 'BT') return 'ð§ð¹';
if($code == 'BV') return 'ð§ð»';
if($code == 'BW') return 'ð§ð¼';
if($code == 'BY') return 'ð§ð¾';
if($code == 'BZ') return 'ð§ð¿';
if($code == 'CA') return 'ð¨ð¦';
if($code == 'CC') return 'ð¨ð¨';
if($code == 'CD') return 'ð¨ð©';
if($code == 'CF') return 'ð¨ð«';
if($code == 'CG') return 'ð¨ð¬';
if($code == 'CH') return 'ð¨ð';
if($code == 'CI') return 'ð¨ð®';
if($code == 'CK') return 'ð¨ð°';
if($code == 'CL') return 'ð¨ð±';
if($code == 'CM') return 'ð¨ð²';
if($code == 'CN') return 'ð¨ð³';
if($code == 'CO') return 'ð¨ð´';
if($code == 'CR') return 'ð¨ð·';
if($code == 'CU') return 'ð¨ðº';
if($code == 'CV') return 'ð¨ð»';
if($code == 'CW') return 'ð¨ð¼';
if($code == 'CX') return 'ð¨ð½';
if($code == 'CY') return 'ð¨ð¾';
if($code == 'CZ') return 'ð¨ð¿';
if($code == 'DE') return 'ð©ðª';
if($code == 'DJ') return 'ð©ð¯';
if($code == 'DK') return 'ð©ð°';
if($code == 'DM') return 'ð©ð²';
if($code == 'DO') return 'ð©ð´';
if($code == 'DZ') return 'ð©ð¿';
if($code == 'EC') return 'ðªð¨';
if($code == 'EE') return 'ðªðª';
if($code == 'EG') return 'ðªð¬';
if($code == 'EH') return 'ðªð';
if($code == 'ER') return 'ðªð·';
if($code == 'ES') return 'ðªð¸';
if($code == 'ET') return 'ðªð¹';
if($code == 'FI') return 'ð«ð®';
if($code == 'FJ') return 'ð«ð¯';
if($code == 'FK') return 'ð«ð°';
if($code == 'FM') return 'ð«ð²';
if($code == 'FO') return 'ð«ð´';
if($code == 'FR') return 'ð«ð·';
if($code == 'GA') return 'ð¬ð¦';
if($code == 'GB') return 'ð¬ð§';
if($code == 'GD') return 'ð¬ð©';
if($code == 'GE') return 'ð¬ðª';
if($code == 'GF') return 'ð¬ð«';
if($code == 'GG') return 'ð¬ð¬';
if($code == 'GH') return 'ð¬ð';
if($code == 'GI') return 'ð¬ð®';
if($code == 'GL') return 'ð¬ð±';
if($code == 'GM') return 'ð¬ð²';
if($code == 'GN') return 'ð¬ð³';
if($code == 'GP') return 'ð¬ðµ';
if($code == 'GQ') return 'ð¬ð¶';
if($code == 'GR') return 'ð¬ð·';
if($code == 'GS') return 'ð¬ð¸';
if($code == 'GT') return 'ð¬ð¹';
if($code == 'GU') return 'ð¬ðº';
if($code == 'GW') return 'ð¬ð¼';
if($code == 'GY') return 'ð¬ð¾';
if($code == 'HK') return 'ðð°';
if($code == 'HM') return 'ðð²';
if($code == 'HN') return 'ðð³';
if($code == 'HR') return 'ðð·';
if($code == 'HT') return 'ðð¹';
if($code == 'HU') return 'ððº';
if($code == 'ID') return 'ð®ð©';
if($code == 'IE') return 'ð®ðª';
if($code == 'IL') return 'ð®ð±';
if($code == 'IM') return 'ð®ð²';
if($code == 'IN') return 'ð®ð³';
if($code == 'IO') return 'ð®ð´';
if($code == 'IQ') return 'ð®ð¶';
if($code == 'IR') return 'ð®ð·';
if($code == 'IS') return 'ð®ð¸';
if($code == 'IT') return 'ð®ð¹';
if($code == 'JE') return 'ð¯ðª';
if($code == 'JM') return 'ð¯ð²';
if($code == 'JO') return 'ð¯ð´';
if($code == 'JP') return 'ð¯ðµ';
if($code == 'KE') return 'ð°ðª';
if($code == 'KG') return 'ð°ð¬';
if($code == 'KH') return 'ð°ð';
if($code == 'KI') return 'ð°ð®';
if($code == 'KM') return 'ð°ð²';
if($code == 'KN') return 'ð°ð³';
if($code == 'KP') return 'ð°ðµ';
if($code == 'KR') return 'ð°ð·';
if($code == 'KW') return 'ð°ð¼';
if($code == 'KY') return 'ð°ð¾';
if($code == 'KZ') return 'ð°ð¿';
if($code == 'LA') return 'ð±ð¦';
if($code == 'LB') return 'ð±ð§';
if($code == 'LC') return 'ð±ð¨';
if($code == 'LI') return 'ð±ð®';
if($code == 'LK') return 'ð±ð°';
if($code == 'LR') return 'ð±ð·';
if($code == 'LS') return 'ð±ð¸';
if($code == 'LT') return 'ð±ð¹';
if($code == 'LU') return 'ð±ðº';
if($code == 'LV') return 'ð±ð»';
if($code == 'LY') return 'ð±ð¾';
if($code == 'MA') return 'ð²ð¦';
if($code == 'MC') return 'ð²ð¨';
if($code == 'MD') return 'ð²ð©';
if($code == 'ME') return 'ð²ðª';
if($code == 'MF') return 'ð²ð«';
if($code == 'MG') return 'ð²ð¬';
if($code == 'MH') return 'ð²ð';
if($code == 'MK') return 'ð²ð°';
if($code == 'ML') return 'ð²ð±';
if($code == 'MM') return 'ð²ð²';
if($code == 'MN') return 'ð²ð³';
if($code == 'MO') return 'ð²ð´';
if($code == 'MP') return 'ð²ðµ';
if($code == 'MQ') return 'ð²ð¶';
if($code == 'MR') return 'ð²ð·';
if($code == 'MS') return 'ð²ð¸';
if($code == 'MT') return 'ð²ð¹';
if($code == 'MU') return 'ð²ðº';
if($code == 'MV') return 'ð²ð»';
if($code == 'MW') return 'ð²ð¼';
if($code == 'MX') return 'ð²ð½';
if($code == 'MY') return 'ð²ð¾';
if($code == 'MZ') return 'ð²ð¿';
if($code == 'NA') return 'ð³ð¦';
if($code == 'NC') return 'ð³ð¨';
if($code == 'NE') return 'ð³ðª';
if($code == 'NF') return 'ð³ð«';
if($code == 'NG') return 'ð³ð¬';
if($code == 'NI') return 'ð³ð®';
if($code == 'NL') return 'ð³ð±';
if($code == 'NO') return 'ð³ð´';
if($code == 'NP') return 'ð³ðµ';
if($code == 'NR') return 'ð³ð·';
if($code == 'NU') return 'ð³ðº';
if($code == 'NZ') return 'ð³ð¿';
if($code == 'OM') return 'ð´ð²';
if($code == 'PA') return 'ðµð¦';
if($code == 'PE') return 'ðµðª';
if($code == 'PF') return 'ðµð«';
if($code == 'PG') return 'ðµð¬';
if($code == 'PH') return 'ðµð';
if($code == 'PK') return 'ðµð°';
if($code == 'PL') return 'ðµð±';
if($code == 'PM') return 'ðµð²';
if($code == 'PN') return 'ðµð³';
if($code == 'PR') return 'ðµð·';
if($code == 'PS') return 'ðµð¸';
if($code == 'PT') return 'ðµð¹';
if($code == 'PW') return 'ðµð¼';
if($code == 'PY') return 'ðµð¾';
if($code == 'QA') return 'ð¶ð¦';
if($code == 'RE') return 'ð·ðª';
if($code == 'RO') return 'ð·ð´';
if($code == 'RS') return 'ð·ð¸';
if($code == 'RU') return 'ð·ðº';
if($code == 'RW') return 'ð·ð¼';
if($code == 'SA') return 'ð¸ð¦';
if($code == 'SB') return 'ð¸ð§';
if($code == 'SC') return 'ð¸ð¨';
if($code == 'SD') return 'ð¸ð©';
if($code == 'SE') return 'ð¸ðª';
if($code == 'SG') return 'ð¸ð¬';
if($code == 'SH') return 'ð¸ð';
if($code == 'SI') return 'ð¸ð®';
if($code == 'SJ') return 'ð¸ð¯';
if($code == 'SK') return 'ð¸ð°';
if($code == 'SL') return 'ð¸ð±';
if($code == 'SM') return 'ð¸ð²';
if($code == 'SN') return 'ð¸ð³';
if($code == 'SO') return 'ð¸ð´';
if($code == 'SR') return 'ð¸ð·';
if($code == 'SS') return 'ð¸ð¸';
if($code == 'ST') return 'ð¸ð¹';
if($code == 'SV') return 'ð¸ð»';
if($code == 'SX') return 'ð¸ð½';
if($code == 'SY') return 'ð¸ð¾';
if($code == 'SZ') return 'ð¸ð¿';
if($code == 'TC') return 'ð¹ð¨';
if($code == 'TD') return 'ð¹ð©';
if($code == 'TF') return 'ð¹ð«';
if($code == 'TG') return 'ð¹ð¬';
if($code == 'TH') return 'ð¹ð';
if($code == 'TJ') return 'ð¹ð¯';
if($code == 'TK') return 'ð¹ð°';
if($code == 'TL') return 'ð¹ð±';
if($code == 'TM') return 'ð¹ð²';
if($code == 'TN') return 'ð¹ð³';
if($code == 'TO') return 'ð¹ð´';
if($code == 'TR') return 'ð¹ð·';
if($code == 'TT') return 'ð¹ð¹';
if($code == 'TV') return 'ð¹ð»';
if($code == 'TW') return 'ð¹ð¼';
if($code == 'TZ') return 'ð¹ð¿';
if($code == 'UA') return 'ðºð¦';
if($code == 'UG') return 'ðºð¬';
if($code == 'UM') return 'ðºð²';
if($code == 'US') return 'ðºð¸';
if($code == 'UY') return 'ðºð¾';
if($code == 'UZ') return 'ðºð¿';
if($code == 'VA') return 'ð»ð¦';
if($code == 'VC') return 'ð»ð¨';
if($code == 'VE') return 'ð»ðª';
if($code == 'VG') return 'ð»ð¬';
if($code == 'VI') return 'ð»ð®';
if($code == 'VN') return 'ð»ð³';
if($code == 'VU') return 'ð»ðº';
if($code == 'WF') return 'ð¼ð«';
if($code == 'WS') return 'ð¼ð¸';
if($code == 'XK') return 'ð½ð°';
if($code == 'YE') return 'ð¾ðª';
if($code == 'YT') return 'ð¾ð¹';
if($code == 'ZA') return 'ð¿ð¦';
if($code == 'ZM') return 'ð¿ð²';
return 'ð³';
}
This code snippet worked for me. Just replace "US"
with whichever valid country code (based on the Regional Indicator Symbol Letters) you like and it will create a String flag
containing the flag emoji for that country. (Reference)
int flagOffset = 0x1F1E6;
int asciiOffset = 0x41;
String country = "US";
int firstChar = Character.codePointAt(country, 0) - asciiOffset + flagOffset;
int secondChar = Character.codePointAt(country, 1) - asciiOffset + flagOffset;
String flag = new String(Character.toChars(firstChar))
+ new String(Character.toChars(secondChar));
This answer helped