Generate random string with native JavaScript


This article goes over how to generate a random string with native JavaScript:

Math.random

Generate string with Math.random:

Math.random().toString(36);
// '0.kq72bptq60s'

Generate string with Math.random without the period:

(Math.random() * 1e17).toString(36);
// 'k7bk5iit9io'

Date.now

Generate string with Date.now:

Date.now().toString(36);
// 'l243fe20'

Generate string with Date.now and Math.random:

(Date.now() * Math.random()).toString(36);
// 'hrl8r78d.88'

Generate string with Date.now and Math.random without the period:

(Date.now() * Math.random() * 1e5).toString(36);
// 'fc7gf0tyc6g'

Random Letter Case

Randomize letter case:

function randomCase(string) {
  return string
    .split('')
    .map((letter) => (Math.random() > 0.5 ? letter.toUpperCase() : letter))
    .join('');
}

Randomize letter case given string generated with Math.random:

randomCase(Math.random().toString(36));
// '0.2vLmxFrq4l2'


Please support this site and join our Discord!