- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
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
Random name generator function in JavaScript
We are required to write a JavaScript function that takes in a number n and returns a random string of length n containing no other than the 26 English lowercase alphabets
Example
Let us write the code for this function −
const num = 8; const randomNameGenerator = num => { let res = ''; for(let i = 0; i < num; i++){ const random = Math.floor(Math.random() * 27); res += String.fromCharCode(97 + random); }; return res; }; console.log(randomNameGenerator(num));
Output
Following is the output in the console −
kdcwping
Note − This is one of many possible outputs. Console output is expected to differ every time/
- Related Articles
- Random color generator in JavaScript
- Random number generator in Java
- How to build a random quote generator using HTML, CSS, and JavaScript?
- JavaScript Generator
- Explain Generator functions in JavaScript?
- What are generator functions in JavaScript?
- What are async generator methods in JavaScript?
- JavaScript Random
- C++ Program to Implement the linear congruential generator for Pseudo Random Number Generation
- How to create a password generator - JavaScript?
- Induction Generator (Asynchronous Generator)
- Generating Random Prime Number in JavaScript
- Generating random hex color in JavaScript
- Generate random string/characters in JavaScript?
- How to execute a JavaScript function using its name in a variable?

Advertisements