🚀 AI SaaS Starter is now live!

50% OFF

Use code FIRST50

Blog/NotesConcept

How to convert HEX to RGB in JavaScript

A quick way to convert HEX to RGB in JavaScript that will help to do the CSS HEX to RGB color conversion programmatically.

Beginner

Anuj Sharma

Last Updated Dec 17, 2025


How to convert HEX to RGB in JavaScript

When working with colors in web development, you may come across the need to convert HEX colors to RGB format. While HEX colors are commonly used in CSS, manipulating colors programmatically requires converting them to RGB or RGBA

In this blog, we'll explore how to convert HEX to RGB in JavaScript programmatically.

Understanding HEX and RGB Colors

Before diving into the conversion process, let's quickly understand what HEX and RGB colors represent:

HEX Color - HEX colors are represented as a six-digit combination of numbers and letters prefixed with a hash (#). For example, #FF0000 represents the color red.

RGB Color - RGB colors are represented by three values for red, green, and blue channels, ranging from 0 to 255. For example, RGB(255, 0, 0) also represents the color red.

Convert HEX to RGB in JavaScript

Converting HEX to RGB involves extracting the red, green, and blue values from the HEX color and converting them to their decimal equivalents.

Here's a code to achieve this conversion:

function hexToRgb(hex) {
    // Remove the hash sign if present
    hex = hex.replace('#', '');
    // Convert HEX to RGB
    const r = parseInt(hex.substring(0, 2), 16);
    const g = parseInt(hex.substring(2, 4), 16);
    const b = parseInt(hex.substring(4, 6), 16);
    return `RGB(${r}, ${g}, ${b})`;
}
// Example usage
const hexColor = '#FF0000';
const rgbColor = hexToRgb(hexColor);
console.log(rgbColor); // Output: RGB(255, 0, 0)

Explanation

In the hexToRgb function:

  1. We remove the # sign from the HEX color string if present.
  2. We extract the red, green, and blue values from the HEX color and convert them to decimal using parseInt(value, <base>) with a base of 16 (hexadecimal).
  3. We return the RGB representation of the color using the extracted values.

Also checkout how to convert RGB to HEX in JavaScript

Tools to Convert HEX to RGB and RGB to HEX

Explore the quick tools for the conversion.

  1. HEX to RGB Converter Online
  2. RGB to HEX Converter Online

🚀

Love this content? Share it!

Help others discover this resource

About the Author

Anuj Sharma

A seasoned Sr. Engineering Manager at GoDaddy (Ex-Dell) with over 12+ years of experience in the frontend technologies. A frontend tech enthusiast passionate building SaaS application to solve problem. Know more about me  🚀

Comments

Be the first to share your thoughts!

Guest User

Please login to comment

0 characters


No comments yet.

Start the conversation!

Share Your Expertise & Help the Community!

Build Your Portfolio

Help the Community

Strengthen Your Skills

Share your knowledge by writing a blog or quick notes. Your contribution can help thousands of frontend developers ace their interviews and grow their careers! 🚀


Other Related Blogs

Implement useToggle() Custom Hook in React (Interview)

Anuj Sharma

Last Updated Nov 23, 2025

Explore code explanation of useToggle() custom hook in react to handle the toggle event efficiently.

Polyfill for map, filter, and reduce in JavaScript

Anuj Sharma

Last Updated Oct 2, 2025

Explore Polyfill for map, filter and reduce array methods in JavaScript. A detailed explanation of Map, filter and reduce polyfills in JS helps you to know the internal working of these array methods.

Flatten Nested Array in JavaScript using Recursion

Anuj Sharma

Last Updated Nov 24, 2025

Understand step by step how to flatten nested array in javascript using recursion, also explore the flatten of complex array of object.

Implement useFetch() Custom Hook in React (Interview)

Anuj Sharma

Last Updated Nov 23, 2025

Find the step-by-step explanation of the useFetch custom hook in React that helps in fetching the data from an API and handling loading, error states.

Implement useThrottle Custom Hook In React (Interview)

Anuj Sharma

Last Updated Nov 23, 2025

Implement useThrottle Custom Hook In React (Interview) to limit the number of APi calls to improve the performance of application.

Master Hoisting in JavaScript with 5 Examples

Alok Kumar Giri

Last Updated Jun 2, 2025

Code snippet examples which will help to grasp the concept of Hoisting in JavaScript, with solutions to understand how it works behind the scene.

Stay Updated

Subscribe to FrontendGeek Hub for frontend interview preparation, interview experiences, curated resources and roadmaps.

FrontendGeek
FrontendGeek

© 2025 FrontendGeek. All rights reserved