Skip to main content
Home / Code Snippets / JavaScript / How To Generate a Random Color in JavaScript

How To Generate a Random Color in JavaScript

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.