How To Generate a Random Color in JavaScript

Avatar of Chris Coyier
Chris Coyier on (Updated on )

Here’s a quicky (there is a PHP version too):

var randomColor = Math.floor(Math.random()*16777215).toString(16);

See the Pen
Generate New Random Hex Color with JavaScript
by Chris Coyier (@chriscoyier)
on CodePen.

If you’d prefer they are a bit more pleasing or need to generator colors that work together, we have an article about that.