Online Playground
Browse tools

How to get the country code from a phone number

Find the country and calling code of a phone number in JavaScript with libphonenumber-js. Includes the full country list, getCountryCallingCode and a live checker.

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.

Which country is this number from?

Try

Processed locally with libphonenumber-js. Nothing you enter is sent to a server.

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).

js
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.

js
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.

js
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.

js
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.

js
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.

FAQ

Questions people ask

What is the difference between country code and calling code?

The country code is usually the two-letter ISO code such as GB. The calling code, or dial code, is the number you dial, such as 44. People use "country code" for either, so check which one a field expects.

Can a phone number tell me where someone is?

No. It only tells you which country issued the number. People travel, and numbers can be ported or forwarded.

Why does a +1 800 number say US?

Toll-free numbers such as +1 800 work across the whole North American plan and are not tied to one country, so the library reports the main country, US. For numbers with a geographic area code, such as +1 416, it returns the right country, here CA.