Convert a number to Hexadecimal in JavaScript

avatar
Borislav Hadzhiev

Last updated: Mar 4, 2024
2 min

banner

# Table of Contents

  1. Convert a number to Hexadecimal in JavaScript
  2. Calling Number.toString() on a number literal
  3. Parsing the hexadecimal value back to a number

# Convert a number to Hexadecimal in JavaScript

To convert a number to hexadecimal, call the toString() method on the number, passing it 16 as the base, e.g. num.toString(16).

The toString method will return the string representation of the number in hexadecimal form.

index.js
const num = 42; // โœ… Convert to Hex const hex = num.toString(16); console.log(hex); // ๐Ÿ‘‰๏ธ "2a" // โœ… Use parentheses when calling toString directly const hex2 = (42).toString(16); console.log(hex2); // ๐Ÿ‘‰๏ธ "2a" // โœ… Parse back to number const parsed = parseInt(hex, 16); console.log(parsed); // ๐Ÿ‘‰๏ธ 42

convert number to hexadecimal

The code for this article is available on GitHub

The Number.toString() method formats the supplied number and returns its string representation.

The only argument we passed to the Number.toString() method is the radix (the base for the conversion).

The hexadecimal numeral system uses 16 distinct symbols, so that's the base value we should specify.

The hexadecimal system uses 16 symbols to represent numbers:

  • "0" to "9" to represent values 0 to 9
  • "a" to "f" (A to F) to represent values 10 to 15. Note that the letters are case-insensitive, so 12AB and 12ab are considered the same value.

When a base of greater than 10 is provided to the toString method, the letters of the alphabet indicate the numerals greater than 9.

For hexadecimal numbers (base 16) - the letters from a to f are used.

const hex1 = (10).toString(16); console.log(hex1); // ๐Ÿ‘‰๏ธ "a" const hex2 = (15).toString(16); console.log(hex2); // ๐Ÿ‘‰๏ธ "f"

# Calling Number.toString() on a number literal

If you call the Number.toString method directly on the number literal, you have to wrap it in parentheses.

index.js
// โœ… Use parentheses when calling toString directly const hex2 = (42).toString(16); console.log(hex2); // ๐Ÿ‘‰๏ธ "2a" // โ›”๏ธ SyntaxError: Invalid or unexpected token const hex3 = 42.toString(16);
The code for this article is available on GitHub

If you don't wrap the numeric literal in parentheses, JavaScript treats the period as a decimal point.

index.js
console.log(5. === 5); // ๐Ÿ‘‰๏ธ true

# Parsing the hexadecimal value back to a number

If you need to parse the hexadecimal value back to a number, use the parseInt() function.

index.js
const num = 42; // โœ… Convert to Hex const hex = num.toString(16); console.log(hex); // ๐Ÿ‘‰๏ธ "2a" // โœ… Parse back to number const parsed = parseInt(hex, 16); console.log(parsed); // ๐Ÿ‘‰๏ธ 42

parsing hexadecimal value back to number

The code for this article is available on GitHub

The parseInt() function parses the supplied string argument and returns an integer of the specified radix.

The function takes the following 2 arguments:

NameDescription
stringA string starting with an integer
radixAn integer between 2 and 36 that represents the radix

The hexadecimal numeral system uses 16 distinct symbols, so that's the radix
value we should specify.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev