How to use .toString() method In Javascript
- In this approach, we will use Math.random() method to generate a number between 0 and 1 and then convert it to base36(which will consist of 0-9 and a-z in lowercase letters).
- using .toString() method. To remove the leading zero and decimal point slice() method will be used and Math.random().toString(36).slice(2) to generate the password.
- For uppercase letters use the same method as the .uppercase() method in concatenation with the previous method.
Example: This example implements the above approach.
function randomPassword() {
console.log(
Math.random().toString(36).slice(2) +
Math.random().toString(36)
.toUpperCase().slice(2));
}
randomPassword();
Output
oq899h92jtfK1IGT3NOIJ
How to Generate a Random Password using JavaScript ?
This article will show you how to generate a random password that may consist of alphabets, numbers, and special characters. This can be achieved in various ways.
These are the following ways:
Table of Content
- Using .charAt() method
- Using .toString() method
- Using a Custom Function with Character Codes
Contact Us