Home
Head's Up: I'm in the middle of upgrading my site. Most things are in place, but there are something missing and/or broken including image alt text. Please bear with me while I'm getting things fixed.

Generate A Random Hex Color In JavaScript

This code generates a random hex color value (including the [TODO: Code shorthand span ] sign)

js
const randomHex = () => {
    const number = Math.floor(Math.random() * 16777215)
    const string = `#${number.toString(16).padStart(6, '0')}`
    return string 
}

console.log(randomHex())
results start

This is based off of the code on [[https : //css - tricks.com/snippets/javascript/random - hex - color/][this page]] but adds ` padStart ` so that colors that would otherwise be less than six characters are padded properly.

There are other approaches on the page as well if you're interested.