How to convert 3-digit color code to 6-digit color code using JavaScript ? Last Updated : 30 Dec, 2022 Comments Improve Suggest changes Like Article Like Report In this article, we are converting a three-digit color HEX code into a six-digit color HEX code using JavaScript. To solve this problem, first, we copy the individual digits and paste them at the consecutive positions. For example - #34E will be converted into #3344EE and #E90 will be converted into #EE9900. Steps to convert 3-digit color code to 6-digit color code: Step 1: The color code is in string format. So, apply the split method on the string. After applying the split method we got an array of elements. JavaScript <script> var digit = "#39E" digit = digit.split("") console.log(digit) </script> Output: ["#","3", "9", "E"] Step 2: Now apply the map method and iterate over the array and return every item with concatenating to itself and check if the item is "#" then do not return the concatenated result, just return the item. JavaScript <script> var digit = "#39E"; digit = digit.split("").map((item)=>{ if(item == "#"){return item} return item + item; }) console.log(digit) </script> Output: ["#", "33", "99", "EE"] Step 3: Now use the join method to convert all the array items into a single string. JavaScript <script> var digit = "#39E" digit = digit.split("").map((item)=>{ if(item == "#"){return item} return item + item; }).join("") console.log(digit) </script> Output: "#3399EE" Step 4: In the above step, we are converting "#39E" but we can see the first element of this code is "#", but if the user does not provide the "#" then you have to check if the first element is "#" then concatenate it with the resulting code. And this is our complete code. JavaScript <script> var digit = "#39E" digit = digit.split("").map((item)=>{ if(item == "#"){return item} return item + item; }).join("") if(digit[0] != "#"){ digit = "#" + digit; } console.log(digit) </script> Output: #3399EE Comment More infoAdvertise with us Next Article How to convert 3-digit color code to 6-digit color code using JavaScript ? D devi_johns Follow Improve Article Tags : JavaScript Web Technologies CSS JavaScript-Questions Similar Reads How to change text color depending on background color using JavaScript? The task is to set the foreground color based on the background color so that it will become visible. Here few of the important techniques are discussed. We are going to use JavaScript. Approach: First, select the random Background color(by selecting random RGB values) or a specific one.Use the YIQ 3 min read How to convert Color Names to Hexcode using JavaScript ? Given a color name and the task is to get the HexCode of the color name. There are a few of the techniques discussed with the help of JavaScript. Approach 1: Store the HexCodes of all possible color names in a JavaScript Object.Compare the given color name with a list of color objects and if a match 4 min read How to create linear-gradient color generator using HTML, CSS and JavaScript ? Creating a background generator using HTML, CSS, and JavaScript is an excellent way to add interactive and visually appealing features to your web projects. This tool allows users to generate gradient backgrounds based on their selected color values. In this article, we'll walk you through the steps 2 min read How to create a Color Generator using HTML CSS and JavaScript ? In this article, we will develop a Color Generator application using HTML, CSS, and JavaScript.In this application, we have created the Multi Format color generator. Users can select Colour Types like RGB, HEX, and CMYK. Use the sliders to customize the color and Inout field in case of HEX and color 6 min read How to create Hex color generator using HTML CSS and JavaScript? Hex color codes are six-digit combinations representing the amount of red, green, and blue (RGB) in a color. These codes are essential in web design and development for specifying colors in HTML and CSS. The hex color generator provides the hex code for any selected color, making it a valuable tool 2 min read Like