Javascript - Number

javascript

What are the two built-in numeric types available in JavaScript?

ECMAScript has two built-in numeric types: Number and BigInt.

The Number type is a double-precision 64-bit binary format IEEE 754 value (numbers between -(2^53 − 1) and 2^53 − 1). In addition to representing floating-point numbers, the number type has three symbolic values: +Infinity, -Infinity, and NaN ("Not a Number").

How can we To check for the largest available value or smallest available value?

To check for the largest available value or smallest available value within ±Infinity, you can use the constants Number.MAX_VALUE or Number.MIN_VALUE.

How can we check if a number is in the double-precision floating-point number range?

Starting with ECMAScript 2015, you are also able to check if a number is in the double-precision floating-point number range using Number.isSafeInteger() as well as Number.MAX_SAFE_INTEGER and Number.MIN_SAFE_INTEGER.

Beyond this range, integers in JavaScript are not safe anymore and will be a double-precision floating point approximation of the value.

Is there such a thing as -0 and +0?

Yes, and no. The number type has only one integer with two representations: 0 is represented as both -0 and +0. (0 is an alias for +0.) In practice, this has almost no impact. For example, +0 === -0 is true. However, you are able to notice this when you divide by zero:

> 42 / +0
Infinity
> 42 / -0
-Infinity
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License