How to Generate Random Birthday Wishes using JavaScript?

In this tutorial, we will learn how to generate a random birthday wish using JavaScript. We will use the Math.random() method to generate a random number and select a birthday wish from an array of wishes.

The Math.random() Method

The Math.random() method is a built-in function in JavaScript that returns a random number between 0 (inclusive) and 1 (exclusive).

console.log(Math.random()); // 0.6789234567
console.log(Math.random()); // 0.1234567890
0.6789234567
0.1234567890

Creating the Array of Birthday Wishes

First, we'll create an array containing various birthday wishes:

var wishes = [
   "Happy birthday! I hope you have a great day!",
   "All the best on your special day!",
   "Wishing you all the happiness in the world on your birthday!",
   "I hope you have a wonderful birthday and a great year ahead!",
   "Wishing you a happy and prosperous birthday!",
   "May your birthday be filled with lots of love, joy and happiness!",
   "Have a fantastic birthday and enjoy every minute!",
   "On this special day, I wish you all the very best, all the joy you can ever have and may you be blessed abundantly today, tomorrow and the days to come! Happy birthday!",
   "May you have a birthday that is as wonderful and amazing as you are!",
   "I hope you have a birthday that is as awesome as you are!"
];

Generating a Random Index

To select a random birthday wish from the array, we need to generate a random index. We use Math.floor() to convert the decimal to a whole number:

// Generate random index between 0 and array length
var arrayLength = 10;
var randomIndex = Math.floor(Math.random() * arrayLength);
console.log("Random index:", randomIndex);
Random index: 7

Complete Example

Below is the complete working example that generates and displays a random birthday wish:

<!DOCTYPE html>
<html>
<head>
   <title>Random Birthday Wishes</title>
</head>
<body>
   <h1>Random Birthday Wish Generator</h1>
   <button onclick="generateWish()">Generate Wish</button>
   <div id="result"></div>
   
   <script>
      var wishes = [
         "Happy birthday! I hope you have a great day!",
         "All the best on your special day!",
         "Wishing you all the happiness in the world on your birthday!",
         "I hope you have a wonderful birthday and a great year ahead!",
         "Wishing you a happy and prosperous birthday!",
         "May your birthday be filled with lots of love, joy and happiness!",
         "Have a fantastic birthday and enjoy every minute!",
         "On this special day, I wish you all the very best, all the joy you can ever have and may you be blessed abundantly today, tomorrow and the days to come! Happy birthday!",
         "May you have a birthday that is as wonderful and amazing as you are!",
         "I hope you have a birthday that is as awesome as you are!"
      ];
      
      function generateWish() {
         var randomIndex = Math.floor(Math.random() * wishes.length);
         document.getElementById("result").innerHTML = "<p><strong>" + wishes[randomIndex] + "</strong></p>";
      }
      
      // Generate a wish automatically when page loads
      generateWish();
   </script>
</body>
</html>

How It Works

The random birthday wish generator works in the following steps:

  1. Math.random() generates a decimal between 0 and 1
  2. Multiply by wishes.length to get a decimal between 0 and array length
  3. Math.floor() converts the decimal to a whole number (index)
  4. Use the random index to select a wish from the array
  5. Display the selected wish in the HTML element

Node.js Version

For console-based output, here's a Node.js version:

var wishes = [
   "Happy birthday! I hope you have a great day!",
   "All the best on your special day!",
   "Wishing you all the happiness in the world on your birthday!",
   "I hope you have a wonderful birthday and a great year ahead!",
   "Wishing you a happy and prosperous birthday!"
];

function getRandomWish() {
   var randomIndex = Math.floor(Math.random() * wishes.length);
   return wishes[randomIndex];
}

console.log("? " + getRandomWish());
? Wishing you all the happiness in the world on your birthday!

Conclusion

Using Math.random() with Math.floor() provides an effective way to generate random birthday wishes. The key is multiplying the random decimal by the array length to get a valid index within the array bounds.

Updated on: 2026-03-15T23:19:00+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements