Thursday, November 13, 2025
HomeLanguagesJavascriptJavaScript to Generate Random Hex Codes of Color

JavaScript to Generate Random Hex Codes of Color

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.

Methods to be used for generating hex codes: 

  • Math.random() method generates any number between 0 and 1 including decimal. 
  • Math.random() * 16 generates no between 0 to 16 including decimal. 
  • Math.floor() method removes the decimal part. 

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

javascript




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


Output

#9B0945

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

RELATED ARTICLES

Most Popular

Dominic
32399 POSTS0 COMMENTS
Milvus
95 POSTS0 COMMENTS
Nango Kala
6765 POSTS0 COMMENTS
Nicole Veronica
11916 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11984 POSTS0 COMMENTS
Shaida Kate Naidoo
6889 POSTS0 COMMENTS
Ted Musemwa
7141 POSTS0 COMMENTS
Thapelo Manthata
6836 POSTS0 COMMENTS
Umr Jansen
6839 POSTS0 COMMENTS