How to convert decimal to hex in JavaScript ?
Given a number and the task is to convert the number from decimal to hex. This can be done by using toString() method. It takes the parameter which is the base of the converted string. In this case the base will be 16.
Syntax:
decimalNumber.toString( radix )
- decimalNumber: It holds the number in decimal format which need to be converted.
- base: It holds the base of number system in which number is converted.
Parameters: It accepts two parameters which are listed below:
Example 1: This example converts 20 to hex number system.
<!DOCTYPE html> <html> <head> <title> How to convert decimal to hex in JavaScript ? </title> </head> <body style = "text-align:center;"> <h1 style = "color:green;" > GeeksForGeeks </h1> <p id="up"></p> <button onclick="myGFG()"> Convert to hex </button> <p id="down" style="color: green"></p> <script> var GFG_Var = 20; var up = document.getElementById("up"); up.innerHTML = GFG_Var; var down = document.getElementById("down"); function myGFG() { var GFG_Var2 = GFG_Var.toString(16); down = document.getElementById("down"); down.innerHTML = "hex of "+ GFG_Var + " is = " + GFG_Var2; } </script> </body> </html> |
Output:
- Before clicking the button:

- After clicking the button:

Example 2: This example converts a number 100 to its octal number system.
<!DOCTYPE html> <html> <head> <title> Decimal to octal number system in JavaScript </title> </head> <body style = "text-align:center;"> <h1 style = "color:green;" > GeeksForGeeks </h1> <p id = "up"></p> <button onclick="myGFG()"> Convert to octal </button> <p id = "down" style="color: green"></p> <script> var GFG_Var = 100; var up = document.getElementById("up"); up.innerHTML = GFG_Var; var down = document.getElementById("down"); function myGFG() { var GFG_Var2 = GFG_Var.toString(8); down = document.getElementById("down"); down.innerHTML = "octal of "+ GFG_Var + " is = " + GFG_Var2; } </script> </body> </html> |
Output:
-
Before clicking on the button:
-
After clicking on the button:
Recommended Posts:
- How to get decimal portion of a number using JavaScript ?
- Round off a number upto 2 decimal place using JavaScript
- How to convert Set to Array in JavaScript?
- How to convert a JavaScript date to UTC?
- How to Convert Array to Set in JavaScript?
- JavaScript | Convert a string to boolean
- How to convert an object to string using JavaScript?
- How to convert a plain object into ES6 Map using JavaScript ?
- Convert a string to an integer in JavaScript
- How to convert string into float in JavaScript?
- How to convert Map keys to an array in JavaScript ?
- Convert URL parameters to a JavaScript Object
- How to convert milliseconds to date in JavaScript ?
- How to convert PHP array to JavaScript or JSON ?
- Convert string into date using JavaScript
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



