Generate random string/characters in JavaScript

前端 未结 30 1757
闹比i
闹比i 2020-11-21 06:34

I want a 5 character string composed of characters picked randomly from the set [a-zA-Z0-9].

What\'s the best way to do this with JavaScript?

30条回答
  •  北海茫月
    2020-11-21 07:32

    One liner:

    Array(15).fill(null).map(() => Math.random().toString(36).substr(2)).join('')
    // Outputs: 0h61cbpw96y83qtnunwme5lxk1i70a6o5r5lckfcyh1dl9fffydcfxffffd69ada9tu9jvqdx864xj1ul3wtfztmh2oz2vs3mv6ej0fe58ho1cftkjcuyl2lfkmxlwua83ibotxqc4guyuvrvtf60naob26t6swzpil
    

提交回复
热议问题