Skip to content
Related Articles
Open in App
Not now

Related Articles

JavaScript to generate random hex codes of color

Improve Article
Save Article
  • Difficulty Level : Basic
  • Last Updated : 03 Jan, 2023
Improve Article
Save Article

What is hex code? 

A hex code is a six-digit, three-byte hexadecimal number used to represent colors in HTML, CSS, and SVG. The bytes represent the red, green, and blue components of the color. The hex codes are an integral part of HTML for web design and are a key way of representing colors digitally.

Functions to be used for generating hex codes: 

Example: In this example, we will generate a random hex color.


    // storing all letter and digit combinations
    // for html color code
    var letters = "0123456789ABCDEF";
    // html color code starts with #
    var color = '#';
    // generating 6 times as HTML color code consist
    // of 6 letter or digits
    for (var i = 0; i < 6; i++)
    color += letters[(Math.floor(Math.random() * 16))];



Note: Output will be different every time the code will get executed. 

My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!