The randomColor() function below will generate a random hex color code and append a preceding # to it automatically.
Function:
function randomColor() { return '#'+Math.floor(Math.random()*16777215).toString(16); }
Math.random()*16777215
: Generates a random number up to 16777215 (ffffff in hex = 16777215 in decimal)
Math.floor(number)
: Removes any trailing decimals from the resulting random number
number.toString(16)
: Converts the number to a hex string
'#'+number
: Appends the number to a # sign
Returns:
#ab45g6