Generate Random Phone Numbers in JavaScript
Complete code tutorial with examples and best practices
[ Code Example - Quick Summary ]
Language: JavaScript
What: Generate random phone numbers in JavaScript using Math.random() and faker-js. Covers US format, international format, and validation for testing.
Try it: Use our interactive Phone-numbers generator or integrate this code into your JavaScript application.
Generate random phone numbers in JavaScript using Math.random() and faker-js. Covers US format, international format, and validation for testing. Looking for other languages? Check our code examples in PHP , Python , Java , C# , C++ , Ruby and Go or use our interactive web generator.
JavaScript Code Example
// Generate random US phone number (xxx) xxx-xxxx
function randomUsPhoneNumber() {
const areaCode = Math.floor(Math.random() * 800) + 200;
const exchange = Math.floor(Math.random() * 800) + 200;
const lineNumber = Math.floor(Math.random() * 9000) + 1000;
return `(${areaCode.toString().padStart(3, "0")}) ${exchange.toString().padStart(3, "0")}-${lineNumber.toString().padStart(4, "0")}`;
}
// Generate random phone number with country code
function randomInternationalPhone(countryCode = "+1") {
const areaCode = Math.floor(Math.random() * 800) + 200;
const exchange = Math.floor(Math.random() * 800) + 200;
const lineNumber = Math.floor(Math.random() * 9000) + 1000;
return `${countryCode} (${areaCode.toString().padStart(3, "0")}) ${exchange.toString().padStart(3, "0")}-${lineNumber.toString().padStart(4, "0")}`;
}
// Generate phone number without formatting
function randomPhoneDigits(length = 10) {
return Array.from({ length }, () => Math.floor(Math.random() * 10)).join("");
}
// Validate US phone number format
function isValidUsPhone(phone) {
const pattern = /^\(\d{3}\) \d{3}-\d{4}$/;
return pattern.test(phone);
}
// Generate multiple phone numbers
function generatePhoneNumbers(count = 10) {
return Array.from({ length: count }, () => randomUsPhoneNumber());
}
// Using faker-js library (npm install @faker-js/faker)
// import { faker } from "@faker-js/faker";
// const phone = faker.phone.number("(###) ###-####"); // (555) 123-4567
// const phoneE164 = faker.phone.number("+1##########"); // +15551234567
console.log("US Phone:", randomUsPhoneNumber());
console.log("International:", randomInternationalPhone("+44"));
console.log("Digits only:", randomPhoneDigits(10));
console.log("Valid:", isValidUsPhone("(555) 123-4567"));
console.log("Batch:", generatePhoneNumbers(3).join(", "));
[EXPLANATION]
JavaScript uses Math.random() for generating area codes (200-999), exchanges (200-999), and line numbers (1000-9999). padStart() ensures leading zeros. RegExp.test() validates patterns. faker-js library offers faker.phone.number() with custom format strings using # for digits. Array.from() with length creates arrays for batch generation. Template literals provide clean string formatting.
Expected Output
US Phone: (555) 234-5678 International: +44 (555) 234-5678 Digits only: 5551234567 Valid: true Batch: (234) 567-8901, (345) 678-9012, (456) 789-0123
Common Use Cases
- Generate phone numbers for React/Vue user registration forms
- Create test data for Node.js contact management APIs
- Populate frontend demos with realistic phone numbers
- Test phone input validation in web applications
- Generate mock data for SMS service testing
Important Notes
-
padStart(3, "0")pads numbers with leading zeros to 3 digits - faker-js format string: # = any digit (0-9), ! = non-zero digit (1-9)
-
Array.from()with map function enables functional-style generation - E.164 format (+[country][number]) is recommended for international numbers
-
RegExp.test()is faster thanString.match()for validation
Try Our Interactive Generator
Don't want to write code? Use our free web-based Phone-numbers generator with instant results.
TRY PHONE-NUMBERS GENERATOR →Other Programming Languages
View Phone-numbers generation code examples in PHP
View Phone-numbers generation code examples in Python
View Phone-numbers generation code examples in Java
View Phone-numbers generation code examples in C#
View Phone-numbers generation code examples in C++
View Phone-numbers generation code examples in Ruby
View Phone-numbers generation code examples in Go