Random alphanumeric string

JavaScript, String, Random · Oct 22, 2020

Generates a random string with the specified length.

const randomAlphaNumeric = length => {
  let s = '';
  Array.from({ length }).some(() => {
    s += Math.random().toString(36).slice(2);
    return s.length >= length;
  });
  return s.slice(0, length);
};

randomAlphaNumeric(5); // '0afad'

More like this

  • JavaScript Random Value Generators

    Quickly and easily generate random integers, strings, booleans, arrays and hex color codes using JavaScript.

    Collection · 7 snippets

  • String is alphanumeric

    Checks if a string contains only alphanumeric characters.

    JavaScript, String · Oct 20, 2020

  • Pad string

    Pads a string on both sides with the specified character, if it's shorter than the specified length.

    JavaScript, String · Oct 22, 2020

  • Truncate string at whitespace

    Truncates a string up to specified length, respecting whitespace when possible.

    JavaScript, String · Oct 21, 2020