# How to convert Hex to RGBA value using JavaScript ?

• Last Updated : 24 Jan, 2023

Given a color hex code and the task is to convert Hex Value to RGBA value. Here are few of the mostly techniques discussed with the help of JavaScript.

Approach 1:

• First check the passed Hex value is valid or not through a Regular Expression.
• Then get the hex value content after ‘#’ by using .slice() method and use .split() method to get the every character of the hex value in the array.
• If the length of the array is 3(eg. #fff, #aaa) then store them as(eg. #ffffff, #aaaaaa respectively) to make the length 6.
• Transform the code to Hexadecimal as the first 2 character belongs to Red Value, the mid 2 character belongs to Green value, and so on.

Example: This example uses the approach discussed above.

## html



GeeksforGeeks

Output:

How to convert Hex to RGBA value using JavaScript ?

Approach 2:

• Use .slice() method to get the first 2 characters and convert them to Hexadecimal.
• Repeat step 1 for every 2 characters for the rest of the code.
• Append them together and return the final value.

Example: This example uses the approach discussed above.

## html



Output:

How to convert Hex to RGBA value using JavaScript ?

My Personal Notes arrow_drop_up