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.

Preorder my new course on Web Components! Want to learn how to build Web Components from scratch, master best practices, and more? Preorder today and get $100 off of the launch price.


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