Skip to content

Convert RGB to Hex in JavaScript

Advertisements

Convert RGB to Hex in JavaScript with the following code snippet.

const rgbToHex = (r, g, b) =>
  "#" + ((1 << 24) + (r << 16) + (g << 8) + b).toString(16).slice(1);

rgbToHex(0, 51, 255); 
// Result: #0033ff
See also  How to represent negative infinity in JavaScript?

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.