JavaScript Numbers

JavaScript has only one type of number. Numbers can be represented in various ways:

// Integers
42                      // 42 
42.00                   // 42

// Floating Point
3.14                    // 3.14

// Octals (base-8)
0121                    // 81
0o123                   // 81 (ES6)

// Hex (base-16)
0xFF                    // 255
parseInt("0xFF", 16)    // 255

// Exponent (e is an abbreviation for multiply 10ˣ)
7e3                     // 7000
7e-2                    // 0.07

Extra large or extra small numbers can be written with scientific (exponent) notation:

var x = 123e5;          // 12300000
var y = 123e-5;         // 0.00123
// Binary (added ES2015+)
0b1101                  // 13

// Playing with the dot
42.                     // 42
.42                     // 0.42

Number constructor

All of the above numbers have the Number constructor.

typeof 42               // "number"
typeof 7e3              // "number"

var a = 0xFF
a.constructor           // [function: Number]
typeof a                // "number"

Number is an object

Everything in JavaScript is an object, including Number. Proof:

Number.prototype.me = function () {
    return this;
};

let foo = 42;
typeof foo;             // "number"

typeof foo.me();        // "object"

foo.me() + 1            // 43

Special numbers

There are quite a few special numbers:

  • -0—negative zero (usually we use the positive value:+0or just0)
  • NaN(not a number)
  • Infinity and -Infinity

In the Number object you will find quite a few other constants:

// The largest representable number
Number.MAX_VALUE         // 1.7976931348623157e+308

// The smallest representable number
Number.MIN_VALUE         // 5e-324

// Special "not a number" value
Number.NaN               // NaN

// Special negative infinite value; returned on overflow
Number.NEGATIVE_INFINITY // -Infinity

// Special positive infinite value; returned on overflow
Number.POSITIVE_INFINITY // Infinity

// Difference between one and the smallest value greater
// than one that can be represented as a Number
Number.EPSILON           // 2.220446049250313e-16

// Minimum safe integer in JavaScript
Number.MIN_SAFE_INTEGER  // -9007199254740991

// Maximum safe integer in JavaScript
Number.MAX_SAFE_INTEGER  // 9007199254740991

JavaScript Numbers are Always 64-bit Floating Point

Unlike many other programming languages, JavaScript does not define different types of numbers, like integers, short, long, floating-point etc.

All numbers in JavaScript are double-precision floating-point numbers, that is, the 64-bit encoding of numbers specified by the IEEE 754 standard—commonly known as “doubles."

This format stores numbers in 64 bits (8 bytes), where the number (the fraction) is stored in bits 0 to 51, the exponent in bits 52 to 62, and the sign in bit 63:

Value (aka Fraction/Mantissa) Exponent Sign
52 bits (0 - 51) 11 bits (52 - 62) 1 bit (63)

Precision

Doubles have a 52-bit mantissa, which is enough to store integers up to about 9✕10¹⁵ precisely.

Integers (numbers without a period or exponent notation) are accurate up to 15 digits.

var x = 999999999999999;   // x will be 999999999999999
var y = 9999999999999999;  // y will be 10000000000000000

Maximum number of decimals is 17, but floating point arithmetic is not always 100% accurate:

var     x = 0.2 + 0.1;                 // x will be 0.30000000000000004

// solution is to multiply then divide
var x = (0.2 * 10 + 0.1 * 10) / 10;    // x will be 0.3

(source: https://www.w3schools.com/js/js_numbers.asp | http://www.informit.com/articles/article.aspx?p=1997934&seqNum=2 | https://www.codementor.io/johnnyb/the-magic-of-numbers-in-javascript-du1087lhb)

results matching ""

    No results matching ""