Number Base Converter

2 (BIN) 8 (OCT) 10 (DEC) 16 (HEX)
  • Only supports conversions between integers
  • The basic data class Number in JavaScript is a double-precision floating-point number, which can be converted to a number in the range of -2^53~2^53, i.e. -9007199254740991~9007199254740991 in decimal.

What is the binary number system?

The binary number system, also known as base 2, is a number system consisting of only two numbers 0 (zero) and 1 (one) abbreviated as BIN. Basically, 0s and 1s in binary represent ON and OFF respectively.

The binary number system has a very important role in computer programming. Computers and computer-based devices use the binary number system to calculate and store various types of digital data, such as text, audio, videos, and computer software using only 0s and 1s as known as binary files.

 

How to convert binary to decimal in JavaScript

You can easily convert a binary number to decimal in newer versions of JavaScript (i.e. ECMAScript 6 and newer) just by adding the prefix 0b to a binary number, and its value will automatically be converted to decimal as you can see in the example below.

const bin = 0b101010;

console.log(bin); // 42
console.log(bin === 42); // true

There is also an old solution to convert a binary number to decimal that can be used in older versions of JavaScript supported by most browsers. You need to convert a binary number to a string first and then use the built-in parseInt function with the radix set to 2 to convert the binary string to decimal.

// Put a binary number in the quotation marks to make it a string,
// and set the radix in the second parameter to 2 (binary).
const dec = parseInt('101010', 2);

console.log(dec); // 42
 

What is the octal number system?

The octal number system, also known as base 8, is a number system consisting of 8 digits from 0 to 7 01234567. In computer programming, the octal number system is often used to shorten binary numbers into a group of 3 digits.

Moreover, Linux and UNIX-based systems use octal to set file and directory permissions via the chmod command; e.g. 600, 644, and 777.

 

How to convert octal to decimal in JavaScript

In modern versions of JavaScript (ECMAScript 6 and newer), you can assign an octal number to a variable by prefixing it with 0o. The value will be converted to decimal automatically as seen in the following.

const oct = 0o52;

console.log(oct); // 42
console.log(oct === 42); // true

Another traditional solution is to use the built-in parseInt function to parse an octal number in a string format with radix 8 as the second parameter to get the decimal value. For example, converting 52 in octal using the parseInt function will result in 42 in decimal like so. Please note that the input octal must be a string.

// Put an octal number in the quotation marks to make it a string,
// and set the radix in the second parameter to 8 (octal).
const dec = parseInt('52', 8);

console.log(dec); // 42
 

What is the decimal number system?

The decimal numbering system, as known as base 10, is a number system consisting of numbers from 0 to 9 0123456789. Basically, it's the base system that we use to naturally count things in daily life as it's perfect for humans which have 10 fingers.

We use the numbers zero to nine to represent numeric values in the decimal number system. However, there is no single digit that represents the value "ten". The number "10" actually consists of "1" and "0" which means 1 ten and 0 ones. Therefore, there is no single digit for ten in the decimal number system.

 

How to convert decimal to binary in JavaScript

You can convert a decimal number back to a binary string using the built-in toString method and passing radix 2 (binary) as the parameter. The result will be a binary number in a string format.

// Set the radix to 2.
const bin = (42).toString(2);

console.log(bin); // "101010"
 

How to convert decimal to octal in JavaScript

Similar to converting decimal to binary, you can use the built-in toString method to convert a decimal number back to a octal string with radix 8 (octal) as the parameter. Please note that the result is a string not integer.

// Set the radix to 8.
const oct = (42).toString(8);

console.log(oct); // "52"
 

How to convert decimal to hexadecimal in JavaScript

Just like converting decimal to any other bases, you can convert a decimal number to a hexadecimal string using the built-in toString method and passing radix 16 (hexadecimal) as the parameter.

// Set the radix to 16.
const hex = (42).toString(16);

console.log(oct); // "2a"
 

What is the hexadecimal number system?

The hexadecimal number system, also known as base 16, is a number system that uses 16 possible digits to represent numbers.

We use hexadecimal which is a complex number system to manage computers, such as addressing memory locations.

 

How to convert hexadecimal to decimal in JavaScript

You can use the prefix 0x to convert a hexadecimal number to decimal with ease in modern versions of JavaScript. The decimal value will be added to a variable when assigned with the prefix 0x.

const hex = 0x2a;

console.log(hex); // 42
console.log(hex === 42); // true

Just like any other bases, you can use the built-in parseInt function to convert a hexadecimal number in a string format with radix set to 16 to get the decimal value as seen in the following example.

// Put a hexadecimal number in the quotation marks to make it a string,
// and set the radix in the second parameter to 16 (hexadecimal).
const dec = parseInt('2a', 16);

console.log(dec); // 42