A phone number has two things people call the country code: the calling code (44 for the UK) and the country (GB). libphonenumber-js returns both. This guide shows how to get each one and how to list every country.
Get the country and calling code from a number
Parse the number. Then read country (the two-letter ISO code) and countryCallingCode (the digits after the plus sign).
import { parsePhoneNumberFromString } from 'libphonenumber-js'
const phone = parsePhoneNumberFromString('+81 3 1234 5678')
phone.country // 'JP'
phone.countryCallingCode // '81'
phone.nationalNumber // '312345678'When country is undefined
A few calling codes belong to no country. They are called non-geographic: +800 (universal toll-free), +808, +870, +878, +881, +882, +883, +888 and +979. For these, phone.country is undefined and phone.isNonGeographic() is true. Unlike Google's library, libphonenumber-js does not return the placeholder 001.
const phone = parsePhoneNumberFromString('+800 1234 5678')
phone.country // undefined
phone.isNonGeographic() // true
phone.countryCallingCode // '800'Countries that share a calling code
Several countries share one code, and the library works out which one a number belongs to from its first digits. +1 covers the US, Canada and many Caribbean nations, and +7 covers Russia and Kazakhstan.
parsePhoneNumberFromString('+1 416 555 0123').country // 'CA' (Toronto area code)
parsePhoneNumberFromString('+1 876 123 4567').country // 'JM' (Jamaica)
parsePhoneNumberFromString('+7 701 123 4567').country // 'KZ'
parsePhoneNumberFromString('+7 840 123 4567').country // 'RU'Get the calling code for a country
Go the other way with getCountryCallingCode. It takes the ISO code and returns the digits.
import { getCountryCallingCode } from 'libphonenumber-js'
getCountryCallingCode('DE') // '49'List every supported country
getCountries returns all ISO codes the library knows. Combine it with Intl.DisplayNames to show names in the visitor's language.
import { getCountries, getCountryCallingCode } from 'libphonenumber-js'
const names = new Intl.DisplayNames(['en'], { type: 'region' })
const list = getCountries().map((c) => ({
code: c,
name: names.of(c),
dial: '+' + getCountryCallingCode(c)
}))Look it up without writing code
Our country code lookup shows the calling code, exit code and trunk prefix for every country. The country checker above works on numbers.