Generate Random IP Addresses in JavaScript
Complete code tutorial with examples and best practices
[ Code Example - Quick Summary ]
Language: JavaScript
What: Generate random IPv4 and IPv6 addresses in JavaScript for frontend and Node.js applications. Includes validation, private IP ranges, and network testing utilities.
Try it: Use our interactive Ip-addresses generator or integrate this code into your JavaScript application.
Generate random IPv4 and IPv6 addresses in JavaScript for frontend and Node.js applications. Includes validation, private IP ranges, and network testing utilities. 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 IPv4 address
function randomIpv4() {
return Array.from({length: 4}, () =>
Math.floor(Math.random() * 256)
).join('.');
}
// Generate random private IPv4 (RFC 1918)
function randomPrivateIpv4() {
const ranges = [
[10, () => Math.floor(Math.random() * 256),
() => Math.floor(Math.random() * 256),
() => Math.floor(Math.random() * 256)],
[172, () => Math.floor(Math.random() * 16) + 16,
() => Math.floor(Math.random() * 256),
() => Math.floor(Math.random() * 256)],
[192, 168, () => Math.floor(Math.random() * 256),
() => Math.floor(Math.random() * 256)],
];
const range = ranges[Math.floor(Math.random() * ranges.length)];
return range.map(val => typeof val === 'function' ? val() : val).join('.');
}
// Generate random public IPv4
function randomPublicIpv4() {
let ip;
do {
ip = randomIpv4();
} while (isPrivateIp(ip));
return ip;
}
// Check if IP is private
function isPrivateIp(ip) {
return /^10\./.test(ip) ||
/^172\.(1[6-9]|2[0-9]|3[01])\./.test(ip) ||
/^192\.168\./.test(ip) ||
/^127\./.test(ip) ||
/^0\./.test(ip);
}
// Generate random IPv6 address
function randomIpv6() {
return Array.from({length: 8}, () =>
Math.floor(Math.random() * 0x10000).toString(16).padStart(4, '0')
).join(':');
}
// Generate IPv4 with CIDR notation
function randomIpv4WithCidr(minCidr = 8, maxCidr = 32) {
const ip = randomIpv4();
const cidr = Math.floor(Math.random() * (maxCidr - minCidr + 1)) + minCidr;
return `${ip}/${cidr}`;
}
// Validate IPv4 address
function isValidIpv4(ip) {
const pattern = /^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/;
return pattern.test(ip);
}
// Generate multiple IP addresses
function generateIpAddresses(count = 10, type = 'ipv4') {
return Array.from({length: count}, () =>
type === 'ipv6' ? randomIpv6() : randomIpv4()
);
}
// Usage examples
console.log("Random IPv4:", randomIpv4());
console.log("Private IPv4:", randomPrivateIpv4());
console.log("Public IPv4:", randomPublicIpv4());
console.log("IPv6:", randomIpv6());
console.log("IPv4 with CIDR:", randomIpv4WithCidr());
console.log("Batch:", generateIpAddresses(5).join(', '));
[EXPLANATION]
This implementation uses Math.random() with Array.from() for generating IP octets. IPv4 addresses use 4 octets (0-255), while IPv6 uses 8 groups of 4 hexadecimal digits. Private IP detection uses regex to match RFC 1918 ranges. The padStart() method ensures IPv6 groups are zero-padded to 4 digits. For Node.js, you can use the built-in net module for validation, or packages like ip or faker for more features.
Expected Output
Random IPv4: 198.51.100.42 Private IPv4: 192.168.15.234 Public IPv4: 203.0.113.89 IPv6: 2001:0db8:85a3:0000:0000:8a2e:0370:7334 IPv4 with CIDR: 198.51.100.42/24 Batch: 203.0.113.15, 198.51.100.89, 192.0.2.45, 198.51.100.123, 203.0.113.67
Common Use Cases
- Mock network API responses in frontend applications
- Generate test data for IP geolocation features
- Create sample server logs for monitoring dashboards
- Test rate limiting and IP-based access control
- Populate network configuration UIs with realistic data
Important Notes
-
For Node.js, use the
netmodule:net.isIPv4()andnet.isIPv6() -
Math.random()is not cryptographically secure; usecrypto.randomBytes()for security testing -
Consider using
faker.internet.ip()orfaker.internet.ipv6()from @faker-js/faker - IPv6 can be compressed but this example shows full uncompressed format
- For production, use test IP ranges like 192.0.2.0/24, 198.51.100.0/24, 203.0.113.0/24
Try Our Interactive Generator
Don't want to write code? Use our free web-based Ip-addresses generator with instant results.
TRY IP-ADDRESSES GENERATOR →Other Programming Languages
View Ip-addresses generation code examples in PHP
View Ip-addresses generation code examples in Python
View Ip-addresses generation code examples in Java
View Ip-addresses generation code examples in C#
View Ip-addresses generation code examples in C++
View Ip-addresses generation code examples in Ruby
View Ip-addresses generation code examples in Go