Two ways to generate random color - Daily JavaScript #5

Two ways to generate random color - Daily JavaScript #5

ยท

1 min read

Play this article

Why not make your own color generator ๐ŸŒˆ?

Method 1 - Long method

// Generate random color in hex format
const randomColor = () => {
    const letters = "0123456789ABCDEF";
    let color = "#";
    for (let i = 0; i < 6; i++) {
        color += letters[Math.floor(Math.random() * 16)];
    }
    return color;
}

Method 2 - Short and Best Method

const randomColor = Math.floor(Math.random()*16777215).toString(16);
console.log("#" + randomColor)

Thank you for reading, have a nice day!

ย