Convert a number to a hexadecimal string with:

hexString = yourNumber.toString(16);

And reverse the process with:

yourNumber = parseInt(hexString, 16);
Answer from Prestaul on Stack Overflow
🌐
Mozilla
developer.mozilla.org › en-US › docs › Web › JavaScript › Guide › Numbers_and_strings
Numbers and strings - JavaScript - MDN Web Docs - Mozilla
Hexadecimal number syntax uses a leading zero followed by a lowercase or uppercase Latin letter "X" (0x or 0X).
🌐
Medium
medium.com › @saumya07013 › exploring-the-world-of-javascript-hexadecimal-operations-7501a24cc3c8
Exploring The World Of Javascript Hexadecimal Operations | by Saumya | Medium
October 1, 2024 - Hex numbers are widely used in ... and other programming languages, hex numbers are often used for memory addresses, color codes (e.g., #FF5733), and bitwise operations....
🌐
30 Seconds of Code
30secondsofcode.org › home › javascript › number › decimal to hexadecimal
JavaScript - Convert decimal number to hexadecimal - 30 seconds of code
September 21, 2022 - const hexToDecimal = hex => parseInt(hex, 16); hexToDecimal('0'); // 0 hexToDecimal('ff'); // 255 · JavaScript · March 5, 2024 · Learn how to easily convert between the various color formats, using JavaScript and a few simple formulas. JavaScript · July 7, 2025 ·
🌐
W3Schools
w3schools.com › js › js_numbers.asp
JavaScript Numbers
JavaScript interprets numeric constants as hexadecimal if they are preceded by 0x.
🌐
DEV Community
dev.to › thecodepixi › what-the-hex-how-to-generate-random-hex-color-codes-in-javascript-21n
What the Hex!? (how to generate random hex color codes in javascript) - DEV Community
February 13, 2020 - A hexadecimal digit includes decimal numbers 0-9, and the letters a-f. These correspond to the decimal (or base 10) digits 0-15. Here's a quick chart: So, if we can find a way to convert a decimal to another number base, all we need to do is ...
Find elsewhere
🌐
Reddit
reddit.com › r/javascript › javascript can't output non-string hex values, can it?
r/javascript on Reddit: JavaScript can't output non-string hex values, can it?
November 3, 2015 -

I'm aware that JS recognizes hex values, e.g. 0xFF0000, but am I correct in thinking that it can't actually output them as anything other than strings or decimal integers? In other words, you could hard code x to be equal to 0xFF0000, but you couldn't set it to that value dynamically, right?

Obviously you could set it to the decimal equivalent, or to a string containing the hex value (i.e. "0xFF0000"), and you can always use toString and parseInt with a base of 16 to translate back and forth, but you can't actually output hex-encoded integers directly. Am I right?

🌐
p5.js
p5js.org › reference › p5 › hex
hex
The first parameter, n, is the number to convert. For example, hex(20), returns the string '00000014'. If an array is passed, as in hex([1, 10, 100]), an array of hexadecimal strings is returned.
🌐
GitHub
gist.github.com › agirorn › 0e740d012b620968225de58859ccef5c
Convert decimal to hex in JavaScript. · GitHub
export const numberToHex = number => { const HEX = 16; return Number(number).toString(HEX).toUpperCase() }
🌐
Modulo
ctp.mkprog.com › en › javascript › hexadecimal_literals
JavaScript | Hexadecimal literal | Easy language reference
JavaScript | Web · Hexadecimal integer constant, also known as base 16 constant. it is from combination of following hexadecimal digits: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, a/A, b/B, c/C, d/D, e/E, f/F · 0xA12F · return - Integers · Hexadecimal literal the possible of use: x = 0x1234; // x = ...
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › p5-js-hex-function
p5.js hex() function - GeeksforGeeks
August 22, 2023 - The hex() function in p5.js is used to convert a number into its hexadecimal notation.
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › how-to-convert-decimal-to-hex-in-javascript
How to convert decimal to hex in JavaScript ? - GeeksforGeeks
July 11, 2025 - Example 1: This example converts 20 to a hex number system. JavaScript · let GFG_Var = 20; function myGFG() { let GFG_Var2 = GFG_Var.toString(16); console.log("Hex of " + GFG_Var + " is = " + GFG_Var2); } myGFG(); Output · Hex of 20 is = 14 · Example 2: This example converts a number 100 to its octal number system.
🌐
GitHub
gist.github.com › valentinkostadinov › 5875467
JavaScript HEX encoding · GitHub
> h = 'hello 🤣'; 'hello 🤣' > const encoded = Buffer.from(h).toString('hex'); undefined > encoded '68656c6c6f20f09fa4a3' > Buffer.from(encoded, 'hex').toString(); 'hello 🤣' >
🌐
GitHub
gist.github.com › faisalman › 4213592
Convert From/To Binary/Decimal/Hexadecimal in JavaScript · GitHub
Convert From/To Binary/Decimal/Hexadecimal in JavaScript · Raw · baseConverter.js · This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
🌐
GitHub
github.com › a2800276 › hexy.js
GitHub - a2800276/hexy.js: hex pretty printing for javascript (node & browser) · GitHub
hexy is an easy to use javascript library to create hex dumps. It works just as well in Node.js, Deno, and browsers.
Starred by 101 users
Forked by 19 users
Languages: JavaScript 69.5% | HTML 30.5%
🌐
W3Resource
w3resource.com › javascript-exercises › javascript-string-exercise-27.php
JavaScript validation with regular expression: Convert ASCII to Hexadecimal format - w3resource
July 22, 2025 - Write a JavaScript function that maps each character’s char code to its hex value and concatenates the results.