As a developer, I often find myself needing to generate realistic-looking data for testing purposes. One common requirement is creating phone numbers that mimic real-world formats. For example, in the United States, phone numbers often follow the 999-999-9999 pattern. This is where the Faker library shines—it allows you to generate fake yet convincing data with ease. Let me walk you through why you’d want this, how to do it, and some practical takeaways.
Thank me by sharing on Twitter 🙏
Why Generate Fake Phone Numbers?
Testing is the cornerstone of reliable software development. Fake phone numbers are crucial when:
- Populating Test Databases: You need realistic-looking placeholder data.
- Simulating User Input: Ensuring form validation works correctly for phone numbers.
- Avoiding Real Data Usage: Using actual numbers can lead to privacy issues.
By generating fake data in controlled formats, we ensure robust testing without compromising user data.
How to Generate Formatted Phone Numbers
To generate phone numbers that look like 999-999-9999, I use the Faker library in conjunction with TypeScript. With its numerify method, you can easily create consistent formats by replacing placeholders (#) with random digits.
Here’s a quick example of how I achieved this:
HP 62 Black Ink Cartridge | Works with Envy 5540, 5640, 5660, 7640, OfficeJet 5740, 8040, OfficeJet Mobile 200, 250 | Instant Ink Eligible | C2P04AN | Packaging May Vary
(as of January 31, 2026 03:50 GMT +00:00 - More infoProduct prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on [relevant Amazon Site(s), as applicable] at the time of purchase will apply to the purchase of this product.)AI Engineering: Building Applications with Foundation Models
$7.99 (as of January 30, 2026 19:55 GMT +00:00 - More infoProduct prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on [relevant Amazon Site(s), as applicable] at the time of purchase will apply to the purchase of this product.)Phantom of the Haunted Bog: A LitRPG Adventure (The Lone Wanderer Book 2)
$5.99 (as of January 30, 2026 19:55 GMT +00:00 - More infoProduct prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on [relevant Amazon Site(s), as applicable] at the time of purchase will apply to the purchase of this product.)import { Faker } from '@faker-js/faker';
const faker = new Faker();
// Generate a phone number in the format 999-999-9999
const formattedPhoneNumber = faker.helpers.replaceSymbolWithNumber('###-###-####');
// Embed the phone number in a custom message
const message = `Contact us at ${formattedPhoneNumber} for assistance.`;
console.log(message);
Let me break it down:
replaceSymbolWithNumber: This method replaces each#with a random digit.- Customization: The
###-###-####format ensures a predictable and realistic structure. - Dynamic Integration: You can embed this number into test messages, logs, or wherever you need.
Conclusion
Using Faker to generate structured fake data is a simple yet powerful way to streamline testing. Phone numbers formatted as 999-999-9999 not only look professional but also help you replicate real-world scenarios. The best part? You can apply similar techniques to create fake addresses, emails, or any other structured data.
Next time you’re setting up a test suite or populating dummy data, try using the numerify method with Faker. It’s quick, reliable, and ensures your testing environment feels authentic.


