Random hex color code

JavaScript, Math, Random · Jan 8, 2021

Generates a random hexadecimal color code.

const randomHexColorCode = () => {
  let n = (Math.random() * 0xfffff * 1000000).toString(16);
  return '#' + n.slice(0, 6);
};

randomHexColorCode(); // '#e34155'

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

  • RGB to hex

    Converts the values of RGB components to a hexadecimal color code.

    JavaScript, String · Nov 3, 2020

  • Random integer array in range

    Generates an array of n random integers in the specified range.

    JavaScript, Math · Oct 22, 2020

  • Random integer in range

    Generates a random integer in the specified range.

    JavaScript, Math · Oct 22, 2020