Skip to main content

Command Palette

Search for a command to run...

Two ways to generate random color - Daily JavaScript #5

Published
β€’1 min read
Two ways to generate random color - Daily JavaScript #5
D

I am 17 years old and a young passionate and self-taught frontend web developer and have an intention to become a successful developer. I usually write about JavaScript and Web Development and share some tips in the articles.

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!

More from this blog

D

Dhairya Shah

119 posts