Random Name Generator Javascript

ApexWolf

ApexWolf

Premium Member
Joined
February 12, 2026
Messages
148
Reaction score
972
Points
93
  • Thread Author
  • #1
In this post, we learn to generate random names using javascript. Here we have the first and last names array. Using the getRandomInt() method we select one name from each array and concatenate them into a full name
Code:
<!DOCTYPE html>
<html lang="en">
<head>
<title>Random Name Generator Javascript</title>
</head>
<body style="background-color: lightcyan;">

<input id="clickMe" type="button" value="Generate Random Name" onclick="generateName();" />

<h2 id="random_name"></h2>


<script>
function capFirst(string) {
return string.charAt(0).toUpperCase() + string.slice(1);
}

function getRandomInt(min, max) {
return Math.floor(Math.random() * (max - min)) + min;
}

function generateName(){
var first_name = ["abandoned","able","absolute","adorable"];

var last_name = ["people","history","way","art","world"];

var name = capFirst(first_name[getRandomInt(0, first_name.length + 1)]) + ' ' + capFirst(last_name[getRandomInt(0, last_name.length + 1)]);
document..innerHTML = name;
}
</script>

</body>
</html>
 
  • Tags
    generator javascript name name generator random random generator web development
  • Top