The underrated .toString() method

All of this value conversion is a lot of mental overhead. Fortunately we can utilize JavaScript to make our lives easier.

Object.prototype.toString(radix)

toString() returns a string representation of an object. It takes an optional radix parameter.

radix is the number of unique digits. It is also known as base

Radix/Base Type
2 binary
10 decimal
16 hexadecimal

Exercise

Write a function that converts from hexadecimal to decimal
function hexToDecimal(hex) {
  return (hex).toString(10);
}
Write a function that converts from decimal to binary
function decimalToBinary(num) {
  return (num).toString(2);
}