
- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Generating random string of specified length 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.
Therefore, let’s write the code for this function −
Example
The code for this will be −
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
The output in the console will be −
kdcwping
Note − This is one of many possible outputs. Console output is expected to differ every time.
- Related Questions & Answers
- Generating random string with a specific length in JavaScript
- Generating Random String Using PHP
- Generating random hex color in JavaScript
- Generating Random Prime Number in JavaScript
- Limiting string up to a specified length in JavaScript
- Python Generate random string of given length
- Generating random numbers in Java
- Generating random numbers in C#
- Generating n random numbers between a range - JavaScript
- Generating a unique random 10 character string using MySQL?
- Generating random number list in Python
- Generating Random Short Id in Node.js
- Padding a string with random lowercase alphabets to fill length in JavaScript
- Generating random Id’s in Python
- Generating random strings until a given string is generated using Python
Advertisements