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 Blog? Share it Now!

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  🚀


Learn Next

Featured

20 Most Asked Custom Hooks in React for Interviews

Top 10 React Performance Optimization Techniques25 Top JavaScript Interview Questions for BeginnersHow to create custom useInfiniteScroll Hook in ReactImplement useThrottle Custom Hook In React

Comments

Be the first to share your thoughts!

Guest User

Please login to comment

0 characters


No comments yet.

Start the conversation!

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  🚀

Share your expertise

Publish a blog or quick notes on topics you know well — your write-up could be the answer someone needs before their next frontend interview.

Build your portfolio

Help the community

Sharpen your skills

Earn goodies

Other Related Blogs

What is javascript:void(0) and How it Works?

Anuj Sharma

Last Updated Jun 15, 2026

A comprehensive explanation about using javascript:void(0) in javascript. When to use javascript:void(0) and how it works with examples of using it with anchor tag.

Understand JavaScript Date Object with Examples (for JavaScript Interviews)

Anuj Sharma

Last Updated Jun 15, 2026

Go through different ways to display dates using javascript date object. It covers examples of date object usage to understand the main concepts of javascript date object.

How to Format Phone Number in JavaScript (JavaScript Interview)

Anuj Sharma

Last Updated Jun 15, 2026

Learn the best & quickest way to format phone number in JavaScript with or without country codes. This will help websites to show the phone numbers in a more human-readable format.

Ultimate guide to REST API calls using Fetch: Machine Coding Essential

Vivek Chavan

Last Updated Jun 15, 2026

You will get a clear understanding about working with any rest api and common concepts asked during interviews

Understanding popstate event in Single Page Applications (SPAs)

Vijay Sai Krishna vsuri

Last Updated Feb 21, 2026

A Quick guide about popstate event in JavaScript, If you’ve ever hit the back button in your browser and wondered how your Single-Page Application knows which view to render, this guide is for you.

25+ Top JavaScript Interview Questions and Answers For Beginners

Anuj Sharma

Last Updated Feb 6, 2026

A comprehensive list of important JavaScript Interview Questions and Answers for Beginners with a detailed explanation of the applied JavaScript concept for in-depth understanding.

How to convert RGB to HEX in JavaScript

Anuj Sharma

Last Updated Dec 17, 2025

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

5 Different Ways to Reload Page in JavaScript (Frontend Interview)

Anuj Sharma

Last Updated Dec 16, 2025

Explore the 5 most efficient ways to refresh or reload page in JavaScript, similar to location.reload(true), and identify the appropriate use cases to reload window using JS programmatically.

Stay Updated

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

FrontendGeek
FrontendGeek

All in One Preparation Hub to Ace Frontend Interviews. Master JavaScript, React, System Design, and more with curated resources.

Consider Supporting this Free Platform

Buy Me a Coffee

Product

HomeFrontend InterviewFrontend JobsQuestionsNewInterview ExperienceBlogsToolsLeaderboardFrontendGeek Chrome extensionGet the extension on the Chrome Web Store →

© 2026 FrontendGeek. All rights reserved