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

React Hook Rules: Why hooks declarations are not allowed inside functions

Frontendgeek

Last Updated Feb 6, 2026

A quick guide to explain an important react interview question, why React Hooks declarations are not allowed inside functions or any conditional blocks with code example.

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.

20+ Frontend Machine Coding Round Interview Questions

Anuj Sharma

Last Updated Feb 6, 2026

A detailed list of 20+ most asked Frontend Machine Coding Round Interview Questions and resources both in JavaScript & React, also covers expected functional/Non-functional requirements.

Mastering React Rendering: How memo and useCallback Eliminate Unnecessary Re-renders

Prateek Labroo

Last Updated Feb 4, 2026

React's rendering is powerful but can become a performance bottleneck in larger apps. Every state change triggers re-renders across your component treeโ€”sometimes unnecessarily. Enter React.memo and useCallback: your optimization superheroes that prevent wasted renders and keep your app snappy.

setTimeout Polyfill in JavaScript - Detailed Explanation

Anuj Sharma

Last Updated Aug 3, 2025

Explore the implementation of setTimeout in JavaScript with a detailed explanation for every step. Understand all scenarios expected to implement the setTimeout polyfill.

Top 10 React Performance Optimization Techniques [React Interview]

Anuj Sharma

Last Updated Feb 21, 2026

Find the top React Performance Optimization Techniques specific to React applications that help to make your react app faster and more responsive for the users along with some bonus techniques.

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 JobsInterview ExperienceBlogsToolsLeaderboardFrontendGeek Chrome extensionGet the extension on the Chrome Web Store โ†’

ยฉ 2026 FrontendGeek. All rights reserved