JavaScript Introduction | Standard Objects | Number Object

Number Representation in JavaScript

JavaScript does not separately distinguish integers and real numbers; it represents all numbers as one kind of real number.
All JavaScript numbers are stored as 64-bit floating-point numbers defined by the IEEE 754 international standard.

64-bit floating-point numbers, also called double-precision floating-point numbers, are stored in memory in the following form.

Bits 0 to 51 Bits 52 to 62 Bit 63
52-bit fraction part 11-bit exponent part 1-bit sign part

The precision of these 64-bit floating-point numbers is valid only up to 15 digits for the integer part and 17 digits for the decimal part.

The following example examines the precision of 64-bit floating-point numbers.

var x = 999999999999999;  // 15-digit integer part
var y = 9999999999999999; // 16-digit integer part
var z = 0.1 + 0.2
x; // 999999999999999
y; // 10000000000000000
z; // 0.30000000000000004

Looking at the value of variable z in the example above shows that an error occurred.
The result of arithmetic operations performed with floating-point numbers always has the possibility of error.
This is not only a JavaScript issue, but a problem in every programming language that represents real numbers with floating-point numbers.

In JavaScript, to remove these errors, you can first convert values to integer form, perform the calculation, and then convert the result back to real-number form.

var z = (0.2 * 10 + 0.1 * 10) / 10; // 0.3

Radix Notation

JavaScript basically uses decimal notation to represent numbers.
However, numbers can also be represented in hexadecimal by using the 0x prefix.

var x = 0xAB; // decimal 171 represented in hexadecimal
var y = 29;   // decimal 29 represented in decimal
x + y;        // both numbers are automatically converted to decimal and calculated. -> 200

As in the example above, during arithmetic operations in JavaScript, all numbers are automatically converted to decimal and calculated.

You can also use the toString() method on a number to convert that number to several radix forms.

var num = 256;
num.toString(2);       // convert to binary: 100000000
num.toString(8);       // convert to octal: 400
num.toString(10);      // convert to decimal: 256
num.toString(16);      // convert to hexadecimal: 100
// returns the result converted to binary as a string.
num.toString(2);       // 100000000
// Because a string is divided by a number, it is automatically converted to decimal and calculated.
(num.toString(2) / 2); // 50000000

The toString() method does not actually change the radix of the number; it returns a string converted to the passed radix.

Infinity

In JavaScript, you can use the Infinity value, which means positive infinity, and the -Infinity value, which means negative infinity.
The Infinity value is a read-only value that users cannot modify arbitrarily, and it is treated as greater than any JavaScript number.

var x = 10 / 0;         // dividing a number by 0 returns Infinity.
var y = Infinity * 1000 // any arithmetic operation with Infinity returns Infinity.
var z = 1 / Infinity    // the reciprocal of Infinity returns 0.
x;                      // Infinity
y;                      // Infinity
z;                      // 0

NaN

NaN (Not A Number) means not a number, and refers to an undefined or unrepresentable value.
It is a read-only value returned when 0 is divided by 0 or when an arithmetic operation is attempted with an operand that cannot be converted to a number.

var x = 100 - "10";     // "10" is automatically converted to a number and calculated.
var y = 100 - "string"; // "string" cannot be converted to a number, so NaN is returned.
var z = 0 / 0;          // 0 cannot be divided by 0, so NaN is returned.
x;                      // 90
y;                      // NaN
z;                      // NaN

Using the isNaN() function, one of JavaScript’s global functions, determines whether the received value is a number.

var x = 100 * "string";
if(isNaN(x)) { // checks whether the passed value is a number.
    document.write("The value of variable x is not a number.");
} else {
    document.write("The value of variable x is a number.");
}

Comparing null, undefined, NaN, and Infinity

JavaScript provides four values that seem somewhat similar but are completely different.

  • null is an object type value that means a value has not yet been determined.
  • undefined, unlike null, is a type and also means that a type has not been determined.
  • NaN is a number type value that means “not a number.”
  • Infinity is a number type value that means “infinity.”

JavaScript is a language with very flexible type checking.
Therefore, the values above are also automatically converted depending on the context as follows.

Value Boolean context Number context String context
null false 0 “null”
undefined false NaN “undefined”
NaN false NaN “NaN”
Infinity true Infinity “Infinity”
typeof null;        // object
typeof undefined;   // undefined
typeof NaN;         // number
typeof Infinity;    // number

Boolean(null);      // false
Boolean(undefined); // false
Boolean(NaN);       // false
Boolean(Infinity);  // true

Number(null);       // 0
Number(undefined);  // NaN
Number(NaN);        // NaN
Number(Infinity);   // Infinity

String(null);       // null
String(undefined);  // undefined
String(NaN);        // NaN
String(Infinity);   // Infinity

Number Object

In JavaScript, numbers are usually represented with number literals.

However, when representing numbers, you can explicitly create a Number object with the new operator.
This Number object is a wrapper object that wraps a numeric value.

var x = 100;             // number literal
var y = new Number(100); // Number object
x;                       // 100
y;                       // 100
typeof x;                // number type
typeof y;                // object type

The equality operator (==) returns true when the literal value and object value are the same.
However, the strict equality operator (===) always returns false because a number literal and a Number object have different types.

var x = 100;             // number literal 100
var y = new Number(100); // Number object 100
x == y;                  // values are the same, so true
x === y;                 // different objects, so false

Creating an object with the new operator requires many additional operations.
Therefore, it is best to represent numbers with number literals whenever possible and use Number objects only as wrapper objects.