Random Name Generator Javascript

  • Thread starter ApexWolf
  • Start date
  • Tagged users None
ApexWolf

ApexWolf

Premium Member
Joined
February 12, 2026
Messages
161
Reaction score
1,318
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>
 

Similar threads

SudoSlayer
Replies
0
Views
229
SudoSlayer
SudoSlayer
Vce.G0d
Replies
1
Views
334
ZennoBossGreu
Z
Vce.G0d
Replies
1
Views
783
ZennoBossGreu
Z
ApexWolf
Replies
0
Views
251
ApexWolf
ApexWolf
  • Tags
    generator javascript name name generator random random generator web development