- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# 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 between 1 and 10. This number will be used to 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).

### Generating a Random Birthday Wish

To generate a random birthday wish, we will use the *Math.random()* method to generate a random number between 1 and 10. This number will be used to select a birthday wish from an array of wishes.

### The Array of Birthday Wishes

The array of birthday wishes will be stored in a variable called "wishes". The array will contain 10 birthday wishes. Below you can see the array of 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!" ];

### Selecting a Random Birthday Wish

To select a random birthday, wish from the array, we will use the Math.random() method to generate a random number between 1 and 10. This number will be used as the index of the array.

For example, if the generated number is 5, then the 5th element in the array will be selected, which is "May your birthday be filled with lots of love, joy, and happiness!".

## Example

Below is the full working code to generate random birthday wishes.

<!doctype html> <html> <head> <title>Examples</title> </head> <body> <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!" ]; var index = Math.floor(Math.random() * 10) + 1; document.getElementById("result").innerHTML = wishes[index] </script> </body> </html>

**Note** − Every time you run the above program it will produce a new wish from the array.

In this tutorial, we have learned how to generate a random birthday wish using JavaScript. We have used the Math.random() function to generate a random number between 1 and 10. This number has been used to select a birthday wish from an array of wishes. We have also learned how to print the selected birthday wish to the console.