Number

JavaScript has only one type of number and that is a 64-bit floating point number.
  • Unlike languages like Java and C++, there is no distinction between whole numbers (integers) and real values (numbers with decimals).
  • JavaScript always stores numbers as double precision floating point, following the international IEEE 754 standard.
let num;
num = 3.14159265;
console.log(typeof num);
num = 3;
console.log(typeof num);

So, an apparent integer is in fact implicitly a float!

const num = 1 / 2;
console.log(num);

There is, like in most programming languages, unavoidable round-off errors:

console.log(0.1 + 0.2);

Number literals

  • Decimal numbers: 42, 0.42
  • Exponential notation: 4.2e-10
  • Hexadecimal: 0x2A
  • Binary: 0b101010
  • Octal: 0o52
// Try any of the above
let num = 42; 
console.log(num);

Special Values

JavaScript has the special values Infinity and -Infinity:

console.log(1 / 0);
console.log(-1 / 0);
console.log(10 * Number.MAX_VALUE);

There is also NaN which means "Not a Number". You get this when e.g. number cannot be parsed from a string, or when a Math operation result is not a real number.

console.log(parseInt("blahblah"));
console.log(Math.sqrt(-1));

You also get NaN when you do weird stuff!

console.log(0 * Infinity);
console.log("JavaScript" / 2);

For a reference, visit MDN web docs on Infinity and NaN.

Number Wrapper object

You can borrow many useful methods defined in Number object. For instance, there is a toString method with optional base argument (between 2 and 36):

const num = 42;
console.log(num.toString(2));

The toPrecision and toExponential can be used for formatting:

const num = 3.14159265;
console.log(num.toPrecision(2));
const val = 1000000; 
console.log(val.toExponential()); 

The Number object has several useful static method and constants as well. For example, there is parseInt method with optional base argument (between 2 and 36):

const num = "0x2A";
console.log(Number.parseInt(num, 16));

There is parseFloat method to convert floating point to number:

const num = "0.001";
console.log(Number.parseFloat(num) * 2);

There are several methods for type checking:

console.log(Number.isFinite(-1 / 0));
console.log(Number.isNaN(1 / 0));
console.log(Number.isInteger(2.1));

And many useful constants:

console.log(Number.MAX_VALUE);
console.log(Number.MIN_VALUE);
console.log(Number.MAX_SAFE_INTEGER);
console.log(Number.MIN_SAFE_INTEGER);
console.log(Number.POSITIVE_INFINITY);
console.log(Number.NEGATIVE_INFINITY);

For a reference, visit Number on MDN web docs.

Big Integer!

JavaScript number type cannot represent integers bigger than 253-1 (Number.MAX_SAFE_INTEGER constant). For integers larger than that limit, you can use a special built-in object, BigInt.

BigInt can be used to represent arbitrary large integers.

BigInt literal have suffix n:

const num = 18889465931478580854784n;
console.log(typeof num);
console.log(num * num);

You can also create BigInt by calling BigInt() function:

const num = Number.MAX_SAFE_INTEGER;
console.log(BigInt(num * num));

You cannot mix BigInt with other numbers

const num = 18889465931478580854784n;
console.log(num + 1);

For a reference, visit BigInt on MDN web docs.