Random hex color code

JavaScript, Math, Random · Jan 7, 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

  • Colors in JavaScript

    Working with color in JavaScript requires some understanding of color formats and conversions. Luckily, this snippet collection's got you covered.

    Collection · 13 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