Skip to main content Accessibility Feedback

getFlagEmoji.js

Get a country’s flag emoji from its two-letter country code.

Source Code

Example

let usa = getFlagEmoji('us');
let japan = getFlagEmoji('jp');
let mexico = getFlagEmoji('mx');

The helper function

/**
 * Get the flag emoji for the country
 * @link https://dev.to/jorik/country-code-to-flag-emoji-a21
 * @param  {String} countryCode The country code
 * @return {String}             The flag emoji
 */
function getFlagEmoji (countryCode) {
	let codePoints = countryCode.toUpperCase().split('').map(char =>  127397 + char.charCodeAt());
	return String.fromCodePoint(...codePoints);
}

How it works

From Jorik…

The flag emoji is a combination of the two unicode region characters, located at unicode position 127462 for the letter A.

The getFlagEmoji() function maps the two letters in the country code to corresponding unicode characters, and returns them as a string.


Find this useful? You can support my work by purchasing an annual membership.