Generate Random Hex Color Code in JavaScript

What is the Hex Color code?

A hex color code in JavaScript is a six-digit string that represents a specific color in the RGB (red, green, blue) color model. The code begins with a hash (#) symbol, followed by three pairs of two-digit hexadecimal values, representing the levels of red, green, and blue respectively. Each two-digit hexadecimal value ranges from 00 to FF, representing the minimum (0) and maximum (255) values for each color.

For example, the hex color code #000000 represents black, because it has no red, green, or blue values. The code #FFFFFF represents white, because it has the maximum value of red, green, and blue. The code #FF0000 represents pure red, because it has the maximum value of red and no green or blue values.

In javascript this is used as a string when working with webpages, it could be used in a css stylesheet or inline with html or can be applied dynamically using JS by manipulating the DOM element's style properties.

Here is the Code on how to generate Hex color code using javascript in just one line.

let hexColor = `#${Math.random().toString(16).slice(2, 8).padEnd(6, 0)}`;
console.log("🚀 ~ file: test.js:5 ~ hexColor", hexColor);