Generate Random Email Addresses in JavaScript - Testing & QA
Complete code tutorial with examples and best practices
[ Code Example - Quick Summary ]
Language: JavaScript
What: Generate random email addresses in JavaScript/Node.js for testing, QA, and mock data with customizable patterns and domains.
Try it: Use our interactive Email generator or integrate this code into your JavaScript application.
Generate random email addresses in JavaScript/Node.js for testing, QA, and mock data with customizable patterns and domains. 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 Email with Random Username
function randomEmail(domain = 'example.com') {
const username = Math.random().toString(36).substring(2, 15);
return `${username}@${domain}`;
}
console.log('Random Email:', randomEmail());
console.log('Custom Domain:', randomEmail('test.org'));
// Generate Email with Readable Username
function readableEmail(domain = 'example.com') {
const adjectives = ['happy', 'sunny', 'clever', 'bright', 'quick'];
const nouns = ['panda', 'tiger', 'eagle', 'dolphin', 'falcon'];
const adj = adjectives[Math.floor(Math.random() * adjectives.length)];
const noun = nouns[Math.floor(Math.random() * nouns.length)];
const num = Math.floor(Math.random() * 900) + 100;
return `${adj}${noun}${num}@${domain}`;
}
console.log('Readable Email:', readableEmail());
// Generate Email from Name Pattern
function emailFromName(firstName, lastName, domain = 'company.com') {
const patterns = [
`${firstName.toLowerCase()}.${lastName.toLowerCase()}`,
`${firstName[0].toLowerCase()}${lastName.toLowerCase()}`,
`${firstName.toLowerCase()}${lastName[0].toLowerCase()}`,
`${firstName.toLowerCase()}_${lastName.toLowerCase()}`,
];
const username = patterns[Math.floor(Math.random() * patterns.length)];
return `${username}@${domain}`;
}
console.log('Name-based:', emailFromName('John', 'Doe'));
// Generate Test Email with Timestamp
function testEmail(domain = 'test.local') {
const timestamp = Date.now();
const random = Math.floor(Math.random() * 9000) + 1000;
return `test_${timestamp}_${random}@${domain}`;
}
console.log('Test Email:', testEmail());
// Generate Multiple Unique Emails
function generateEmails(count, domain = 'example.com') {
return Array.from({ length: count }, (_, i) =>
`user${Date.now()}${i}@${domain}`
);
}
console.log('Multiple Emails:', generateEmails(3));
// Validate Email Format (Basic)
function isValidEmail(email) {
const regex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
return regex.test(email);
}
const email = randomEmail();
console.log('Valid:', isValidEmail(email) ? 'Yes' : 'No');
// Using crypto for more random usernames (Node.js)
// const crypto = require('crypto');
// function cryptoEmail(domain = 'example.com') {
// const username = crypto.randomBytes(8).toString('hex');
// return `${username}@${domain}`;
// }
[EXPLANATION]
JavaScript generates random emails using Math.random() for usernames combined with domain names. toString(36) converts numbers to alphanumeric strings for readable usernames. For unique emails, use Date.now() for timestamp-based uniqueness. Array.from() creates multiple emails efficiently. Name-based patterns use template literals and string methods. For Node.js, crypto.randomBytes() provides cryptographically secure random usernames. Basic email validation uses regex, but for production use libraries like validator.js.
Expected Output
Random Email: k8j2n5p@example.com Custom Domain: x9w3q7r@test.org Readable Email: brighteagle742@example.com Name-based: john.doe@company.com Test Email: test_1703001234567_8392@test.local Multiple Emails: [ 'user17030012345670@example.com', 'user17030012345671@example.com', 'user17030012345672@example.com' ] Valid: Yes
Common Use Cases
- Generate test users for Cypress/Playwright E2E tests
- Create mock data for React/Vue component testing
- Populate MongoDB/PostgreSQL with seed data
- Generate emails for Jest/Mocha unit tests
- Create test accounts for authentication flows
Important Notes
-
Math.random()is not cryptographically secure - Use test domains (.test, .local, example.com) for testing
-
For Node.js, prefer
crypto.randomBytes() - Consider using Faker.js (@faker-js/faker) for production-ready fake data
- Regex validation is basic - use validator.js for robust validation
Try Our Interactive Generator
Don't want to write code? Use our free web-based Email generator with instant results.
TRY EMAIL GENERATOR →Other Programming Languages
View Email generation code examples in PHP
View Email generation code examples in Python
View Email generation code examples in Java
View Email generation code examples in C#
View Email generation code examples in C++
View Email generation code examples in Ruby
View Email generation code examples in Go