How to Convert Decimal to Hexadecimal Number in JavaScript
- Understanding Decimal and Hexadecimal Number Systems
-
Method 1: Using the
toString()
Method - Method 2: Using Bitwise Operators
- Method 3: Using ES6 Template Literals
- Conclusion
- FAQ

In this article, we will see how to convert any decimal number into a hexadecimal number in JavaScript. Understanding this conversion can be particularly useful for developers working with color codes, memory addresses, and other applications where hexadecimal notation is prevalent. By the end of this guide, you’ll be equipped with several methods to perform this conversion effortlessly. Whether you’re a beginner looking to grasp the basics or an experienced coder brushing up on your skills, this article will provide you with practical examples and clear explanations. Let’s dive into the world of number systems and explore how to make this conversion in JavaScript.
Understanding Decimal and Hexadecimal Number Systems
Before we jump into the conversion methods, let’s clarify what decimal and hexadecimal number systems are. The decimal system, or base 10, is the numerical system most commonly used by humans, consisting of digits from 0 to 9. On the other hand, the hexadecimal system, or base 16, uses sixteen symbols: 0-9 for values zero to nine and A-F for values ten to fifteen. This system is widely used in computing because it can represent large binary numbers more succinctly.
Method 1: Using the toString()
Method
One of the simplest ways to convert a decimal number to hexadecimal in JavaScript is by using the built-in toString()
method. This method converts a number to a string in a specified base. By passing 16 as an argument, you can easily convert a decimal number to hexadecimal.
Here’s how you can do it:
function decimalToHex(decimal) {
return decimal.toString(16);
}
console.log(decimalToHex(255));
Output:
ff
In this example, we define a function named decimalToHex
that takes a decimal number as an argument. Inside the function, we call the toString()
method on the decimal number, passing 16 as the base. When we test this function with the decimal number 255, it returns ff
, which is the hexadecimal representation of 255. This method is straightforward and efficient, making it a popular choice among developers for quick conversions.
Method 2: Using Bitwise Operators
Another interesting method to convert a decimal number to hexadecimal is by using bitwise operators. This approach can be a bit more complex but offers a deeper understanding of how numbers are represented in binary and hexadecimal formats.
Here’s a code snippet demonstrating this method:
function decimalToHex(decimal) {
let hex = '';
while (decimal > 0) {
let remainder = decimal % 16;
hex = (remainder < 10 ? remainder : String.fromCharCode(remainder + 87)) + hex;
decimal = Math.floor(decimal / 16);
}
return hex;
}
console.log(decimalToHex(255));
Output:
ff
In this method, we create a function decimalToHex
that initializes an empty string hex
. We use a while
loop to continue processing as long as the decimal number is greater than zero. Inside the loop, we calculate the remainder of the decimal number divided by 16. If the remainder is less than 10, we simply append it to our hex
string. If it’s 10 or more, we convert it to the corresponding hexadecimal character using String.fromCharCode()
. Finally, we return the constructed hexadecimal string. This method showcases how you can manipulate numbers at a lower level, providing insight into the number conversion process.
Method 3: Using ES6 Template Literals
With the introduction of ES6, JavaScript has become more versatile, allowing developers to use template literals for various tasks, including number conversion. You can leverage template literals along with the toString()
method for a more modern approach.
Here’s an example:
function decimalToHex(decimal) {
return `0x${decimal.toString(16)}`;
}
console.log(decimalToHex(255));
Output:
0xff
In this example, we define the decimalToHex
function similarly to the first method. However, this time we use a template literal to prepend 0x
to the hexadecimal string. This is a common convention in programming to denote that a number is in hexadecimal format. When we run this function with the decimal number 255, it returns 0xff
. This method not only provides the conversion but also adheres to standard hexadecimal notation, making it clear to anyone reading the code.
Conclusion
Converting decimal numbers to hexadecimal in JavaScript is a straightforward task, thanks to the various methods available. Whether you prefer the simplicity of the toString()
method, the intricate workings of bitwise operations, or the modern flair of ES6 template literals, JavaScript provides you with the tools you need to achieve this conversion. Understanding these methods will enhance your programming skills and help you work more effectively with different number systems. So, the next time you need to perform a decimal-to-hex conversion, you’ll have the knowledge and confidence to do it seamlessly.
FAQ
- How do I convert a negative decimal number to hexadecimal in JavaScript?
You can convert a negative decimal number to hexadecimal by first converting its absolute value and then adding a negative sign to the result.
-
Is there a built-in JavaScript function for hexadecimal conversion?
Yes, you can use thetoString()
method with 16 as an argument to convert decimal numbers to hexadecimal. -
Can I convert floating-point decimal numbers to hexadecimal in JavaScript?
Yes, you can convert floating-point numbers to hexadecimal, but you may need to handle the fractional part separately. -
What is the significance of hexadecimal numbers in programming?
Hexadecimal numbers are significant in programming because they provide a more human-readable representation of binary data, making it easier to work with memory addresses, color codes, and more. -
Are there any libraries to assist with number conversions in JavaScript?
Yes, there are several libraries available, such asbig.js
ordecimal.js
, which can assist with various number conversions and calculations.
Sahil is a full-stack developer who loves to build software. He likes to share his knowledge by writing technical articles and helping clients by working with them as freelance software engineer and technical writer on Upwork.
LinkedIn