How to generate random string in TypeScript

1 Answer

0 votes
function random_string(size : number) : string {
   const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
   const len = characters.length;
   let rnd = '';
   for (let i = 0; i < size; i++) {
      rnd += characters.charAt(Math.floor(Math.random() * len));
   }
   return rnd;
}
   
console.log(random_string(9));
    
    
    
    
/*
run:
        
"EdXqaejMD" 
   
*/

 



answered Apr 25, 2022 by avibootz
...