How To Make Random Hex Colour In Javascript


In this tutorial you will learn how to create random colours using Javascript.

To create a random colour in Javascript it is a simple function.

function random_colour(){
     return Math.floor(Math.random()*16777215).toString(16);

View the demo to see how this can be used to change the background colour of the page.

Just click the random button and the Javascript will create a new random colour and set this as the background.


Back to top

Learn how to code with Treehouse

  • Learn projects with access to 1000+ videos
  • Practice live with our Code Challenge Engine
  • Get help in our members-only forums

Start with a 7 day free trial