Country Details By Phone
Identifies matching countries and extracts details based on the country dial code prefix in a phone number.
getCountryByPhone
Gets country details by matching the country code in the given phone number.
Function Signature
function getCountryByPhone(phone: number | string): CountryDetails[]Parameters
| Parameter | Type | Description |
|---|---|---|
phone | number | string | The phone number to look up. |
Returns
CountryDetails[]: An array of matching country details. Returns an empty array[]if no matches are found or input is invalid.
Example Usage
playground.ts
Behavior Details
- Normalization: Automatically strips all non-digit characters (
+,-, spaces, parentheses) before matching. - Hyphenated Country Codes: Safely processes and matches country dialing codes that are stored with hyphens (e.g.
'1-242'for Bahamas). - Ambiguous Codes: Since multiple countries can share the same dialing prefix, the function returns an array containing all matching countries.
Types
CountryDetails
type CountryDetails = Readonly<{
country_name: string;
country_code: string;
iso_code_short: string;
iso_code: string;
}>;Last updated: Sun, Jun 14, 2026 07:06:16AM (UTC)
