How to generate a random HEX RGB color code in JavaScript

1 Answer

0 votes
function generateRandomHexColor() {
  const hexChars = "0123456789ABCDEF";
  let hex = "";
  
  for (let i = 0; i < 6; i++) {
    hex += hexChars[Math.floor(Math.random() * 16)];
  }
  
  return hex;
}

console.log(`Random HEX Color: #${generateRandomHexColor()}`); 



/*
run:

Random HEX Color: #8D279F

*/

 



answered Oct 9, 2025 by avibootz
...