Files
30-seconds-of-code/snippets/randomString.md
2020-10-05 01:25:58 +05:45

427 B

title, tags
title tags
randomString math,random,beginner

Returns a random string of the specified length.

  • Math.random() generate a random number
  • ToString(16) converts the number into a text string
  • Finally, the substr(2,8) extracts chracters between the positions 2 and supplied length.
const randomString = (length) => Math.random().toString(20).substr(2, length)
randomString(5); // '0afad'