🏆 FrontendGeek Leaderboard is live !! Signin & Check your position on the leaderboard

Blog/NotesConcept

How to Format Phone Number in JavaScript (JavaScript Interview)

Learn the best & quickest way to format phone number in JavaScript with or without country codes. This will help websites to show the phone numbers in a more human-readable format.

Beginner

Anuj Sharma

Last Updated Jan 9, 2025


Advertisement

Displaying a phone number or taking a phone number as input is a widespread requirement for any website. While showing the phone number, it's a common problem to show phone numbers in a human-readable format, making it very easy to follow.

That is why it's important to learn how to format phone numbers in JavaScript with or without Country Code. Here is how you can do it in javascript

Table of Content

  1. Expected Phone Number and Formatted Output
  2. Format Phone Number Without Country Code
  3. Format Phone Number With Country Code

# Expected Phone Number and Formatted Output

// Phone number without country code
input: 9674140087
output: (967) 414-0087

// Phone number with country code
input: 919079151237
output:  +91 (907) 915-1237

# Format Phone Number Without Country Code

Phone numbers without country codes should have a length of 10 digits. As part of the first step of formatting a phone number, all the non-numeric digits should be removed like dash (-) and check for the valid phone number length without country code. In the second step, formatting is done by using the required regex expression /^(\d{3})(\d{3})(\d{4})$/ and returned the formatted phone number.

Here is the code to format the phone number without the country code

Code:

function formatPhoneNumberWithoutCC(phoneNumber) {
	// Remove all non-digit characters
	const cleaned = phoneNumber.replace(/\D/g, '');
	const ccLength = cleaned.split('').length;

	if(ccLength > 10 || ccLength < 10){
		console.error("Invalid phone number length!!");
		return;
	}

	// Match common US phone number formats
	const match = cleaned.match(/^(\d{3})(\d{3})(\d{4})$/);
	if (match) {
		return `(${match[1]}) ${match[2]}-${match[3]}`;
	}

	return phoneNumber; // Return original if no match
}

Tests:

Here are the tests, with 10-digit phone numbers, phone numbers with special chars and invalid phone numbers.

console.log(formatPhoneNumberWithoutCC('9674140087')); 
// (967) 414-0087

console.log(formatPhoneNumberWithoutCC('967-414-0087'));
// (967) 414-0087

console.log(formatPhoneNumberWithoutCC('invalid')); 
// Invalid phone number length!!

# Format Phone Number with Country Code

Phone numbers with country codes should have a length between 11 to 12 digits to represent 1-digit or 2-digit country codes. As part of the first step of formatting a phone number, all the non-numeric digits should be removed like dash (-) and check for the valid phone number length without country code.

In the second step, formatting is done by using the required regex expression /^(\d{1}) (\d{3})(\d{3})(\d{4})$/ for 1-digit country codes and /^(\d{2}) (\d{3})(\d{3})(\d{4})$/ for a 2-digit country code and returned the formatted phone number.

Here is the code to format the phone number with the country code

Code:

function formatPhoneNumberWithCC(phoneNumber) {
	// Remove all non-digit characters
	const cleaned = phoneNumber.replace(/\D/g, '');  
	const phoneNumberLength = cleaned.split('').length;
	let matchWithCountryCode = null;

	if(phoneNumberLength < 11 || phoneNumberLength > 12){
		console.error("Country code missing Or Invalid phone number");
		return;
	} else if(phoneNumberLength === 11) {
		// Handle 1 digit country code
		matchWithCountryCode = cleaned.match(/^(\d{1})(\d{3})(\d{3})(\d{4})$/)
	} else if(phoneNumberLength > 11 && phoneNumberLength <= 12){
		// Handle 2 digit country code
		matchWithCountryCode = cleaned.match(/^(\d{2})(\d{3})(\d{3})(\d{4})$/)
	}

	if (matchWithCountryCode) {
		return `+${matchWithCountryCode[1]} (${matchWithCountryCode[2]}) ${matchWithCountryCode[3]}-${matchWithCountryCode[4]}`;
	}

	return phoneNumber; // Return original if no match
}

Tests:

Here are the tests, with one or two-digit country codes and phone numbers without country codes.

console.log(formatPhoneNumberWithCC('19673140087')); 
//Output: +1 (967) 314-0087

console.log(formatPhoneNumberWithCC('919673140087')); 
//Output: +91 (967) 314-0087

console.log(formatPhoneNumberWithCC('9073140087')); 
//Output: Country code missing Or Invalid phone number

Share this post now:

Advertisement

💬 Comments (0)

Login to comment

Advertisement

Flaunt You Expertise/Knowledge & Help your Peers

Sharing your knowledge will strengthen your expertise on topic. Consider writing a quick Blog/Notes to help frontend folks to ace Frontend Interviews.

Advertisement


Other Related Blogs

Master Hoisting in JavaScript with 5 Examples

Alok Kumar Giri

Last Updated Jun 2, 2025

Code snippet examples which will help to grasp the concept of Hoisting in JavaScript, with solutions to understand how it works behind the scene.

setTimeout Polyfill in JavaScript - Detailed Explanation

Anuj Sharma

Last Updated Aug 3, 2025

Explore the implementation of setTimeout in JavaScript with a detailed explanation for every step. Understand all scenarios expected to implement the setTimeout polyfill.

Best Cheat Sheet for Frontend Machine Coding Interview Round

Anuj Sharma

Last Updated Jun 13, 2025

A comprehensive cheat sheet for the Frontend Machine Coding Interview Round, which helps to revise all the important machine coding & UI design concepts before the interview.

20+ Frontend Machine Coding Interview Questions (JS + React)

Anuj Sharma

Last Updated Jun 27, 2025

A detailed list of 20+ most asked Frontend Machine Coding Interview Questions and resources both in JavaScript & React. Also covers expected functional/Non-functional requirements in this Interview.

Understanding popstate event in Single Page Applications (SPAs)

Vijay Sai Krishna vsuri

Last Updated Aug 21, 2025

A Quick guide about popstate event in JavaScript, If you’ve ever hit the back button in your browser and wondered how your Single-Page Application knows which view to render, this guide is for you.

JavaScript Essentials: Call, Apply and Bind Methods Explained with Polyfills

Last Updated Aug 21, 2025

A beginner-friendly guide to understanding call, apply, and bind methods in JavaScript, along with step-by-step call, apply and bind polyfill implementations that are often asked in interviews.

FrontendGeek
FrontendGeek

© 2024 FrontendGeek. All rights reserved