Open In App

JavaScript Program to generate one-time password (OTP)

Improve
Improve
Like Article
Like
Save
Share
Report

A one-time Password (OTP) is a password that is valid for only one login session or transaction on a computer or a digital device. Nowadays they are used in almost every service like Internet Banking, online transactions, etc. They are generally a combination of 4 or 6 numeric digits or a 6-digit alphanumeric. The random function is used to generate random OTP which is predefined in the Math library. This article describes how to generate OTP using JavaScript.

Used Function to Generate OTP in JavaScript

  • Math.random(): This function returns any random number between 0 to 1.
  • Math.floor(): It returns the floor of any floating number to an integer value.

Using the above function pick a random index of a string array that contains all the possible candidates of a particular digit of the OTP.

These are the types of OTP generation programs:

 

Program to generate a one-time password (OTP) Only Numeric

In this method, we are only generating OTP with Numeric value only. We are storing all the digits(0 to 9) in a variable called digits and by the use of the functions Math.floor() and Math.random in a for loop, we are generating the 4-digit numeric OTP.

Example: This example generates a 4-digit Numeric OTP.

javascript




// Function to generate OTP
function generateOTP() {
  
    // Declare a digits variable
    // which stores all digits 
    let digits = '0123456789';
    let OTP = '';
    let len = digits.length
    for (let i = 0; i < 4; i++) {
        OTP += digits[Math.floor(Math.random() * len)];
    }
     
    return OTP;
}
  
console.log("OTP of 4 digits: ")
console.log(generateOTP());


Output

OTP of 4 digits: 
6499


Program to generate a one-time password (OTP) with Alphanumeric

In this method, we are only generating OTP with Alphanumeric value only. We are storing all the digits(0 to 9) in a variable called digits and by the use of the functions Math.floor() and Math.random in a for loop, we are generating the 6-digit Alphanumeric OTP.

Example: This example generates OTP with special characters.

javascript




// Function to generate OTP 
function generateOTP() {
  
    // Declare a digits variable 
    // which stores all digits 
    let digits = 
'0123456789abcdefghijklmnopqrstuvwxyz';
    let OTP = '';
    let len = digits.length
    for (let i = 0; i < 6; i++) {
        OTP += digits[Math.floor(Math.random() * len)];
    }
    return OTP;
}
  
console.log("OTP of 6 digits: ")
console.log(generateOTP());


Output

OTP of 6 digits: 
459956


Program to generate one-time password (OTP) with Special Characters

In this method, we are only generating OTP with Special characters. We are storing all the digits(0 to 9) in a variable called digits and by the use of the functions Math.floor() and Math.random in a for loop, we are generating the 6-digit Alphanumeric having special characters OTP.

Example: This example generates Alphanumeric having special characters OTP of length 6.

javascript




// Function to generate OTP 
function generateOTP() {
  
    // Declare a digits variable 
    // which stores all digits 
    let digits = 
'0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!@#$%^&*()_+';
    let OTP = '';
    let len = digits.length;
    for (let i = 0; i < 6; i++) {
        OTP += digits[Math.floor(Math.random() * len)];
    }
    return OTP;
}
  
console.log("OTP of 6 digits: ")
console.log(generateOTP());


Output

OTP of 6 digits: 
052581




Last Updated : 11 Mar, 2024
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads