Question about random hexadecimal color native javascript

Viewed 5

I'm a beginner non native speaker so first i am sorry if i am asking my question in a weird way !

I did a random color generator and i found multiple way to generate a random hexadecimal value color. But there is one I found really short without really understanding it.

    let randomColor = Math.floor(Math.random()*16777215).toString(16);

this is giving you 6 hexadecimal value you just need to add "#" in front and here you go.

But I don't understand why. If you delete de toString(16) and add it underline it's not working anymore.

    let randomColor = Math.floor(Math.random()*16777215);
    randomColor.toString(16);
    console.log(randomColor);

So I managed to finish my little project but i'm kinda frustrated to not really understand the toString(16) part, to me it was just usefull to transform into string...

Also I don't really understand how this can work without slicing. How the person that wrote this can be absolutely sure it's going to be 6 length 100% of the time with such a random number for math random...

0 Answers
Related