JavaScript Numbers


JavaScript numbers can be written with, or without decimals.

Example

var x = 34.00;    // A number with decimals
var y = 34;       // A number without decimals

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

var x = 123e5;    // 12300000
var y = 123e-5;   // 0.00123

Precision

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

Example

<span id="view"></span>

<script type="text/javascript">
function check() 
  {
   var a = 999999999999999;
   var b = 9999999999999999;
   document.getElementById("view").innerHTML = a + "<br>" + b;
  }
</script>

<input type="button" onclick="check()" value="Check" />
Output

The maximum number of decimals is 17, but floating point arithmetic is not always 100% accurate.

Example

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

To solve the problem above, it helps to multiply and divide.

Example

var x = (0.2 * 10 + 0.1 * 10) ⁄ 10;       // x will be 0.3

Hexadecimal

JavaScript interprets numeric constants as hexadecimal if they are preceded by 0x.

Example

var y = 0xFF;             // y will be 255

By default, Javascript displays numbers as base 10 decimals.

But you can use the toString() method to output numbers as base 16 (hex), base 8 (octal), or base 2 (binary).

Example

var num = 128;
num.toString(16);     // returns 80
num.toString(8);      // returns 200
num.toString(2);      // returns 10000000

Infinity

Infinity (or -Infinity) is the value JavaScript will return if you calculate a number outside the largest possible number.

Example

var myNumber = 2;
while (myNumber != Infinity) {          // Execute until Infinity
    myNumber = myNumber * myNumber;
}

Division by 0 (zero) also generates Infinity.

Example

var a =  2 ⁄ 0;          // a will be Infinity
var b = -2 ⁄ 0;          // b will be -Infinity

Infinity is a number: typeOf Infinity returns number.

typeof Infinity;        // returns "number"

Numbers Can be Objects

Normally JavaScript numbers are primitive values created from literals: var a = 234

But numbers can also be defined as objects with the keyword new: var b = new Number(234)

Example

var a = 234;
var b = new Number(234);

typeof a;               // returns number
typeof b;               // returns object

Number Properties

PropertyDescription
MAX_VALUEReturns the largest number possible in JavaScript
MIN_VALUEReturns the smallest number possible in JavaScript
NEGATIVE_INFINITYRepresents negative infinity (returned on overflow)
POSITIVE_INFINITYRepresents infinity (returned on overflow)
NaNRepresents a "Not-a-Number" value
<span id="view"></span>
<span id="view2"></span>
<span id="view3"></span>
<span id="view4"></span>
<span id="view5"></span>

<script type="text/javascript">
 function check()
 {
document.getElementById("view").innerHTML = Number.MAX_VALUE;
document.getElementById("view2").innerHTML = Number.MIN_VALUE;
document.getElementById("view3").innerHTML = Number.NEGATIVE_INFINITY;
document.getElementById("view4").innerHTML = Number.POSITIVE_INFINITY;
document.getElementById("view5").innerHTML = Number.NaN;
 } 
</script>

<input type="button" onclick="check()" value="Check" />
Output




Number properties belongs to the JavaScript's number object wrapper called Number.


Global Methods

JavaScript global functions can be used on all JavaScript data types.

These are the most relevant methods, when working with numbers.

MethodDescription
Number()Returns a number, converted from its argument.
parseFloat()Parses its argument and returns a floating point number
parseInt()Parses its argument and returns an integer

Number Methods

The Number object contains only the default methods that are part of every object's definition.

MethodDescription
constructor()Returns the function that created this object's instance. By default this is the Number object.
toExponential()Forces a number to display in exponential notation, even if the number is in the range in which JavaScript normally uses standard notation.
toFixed()Formats a number with a specific number of digits to the right of the decimal.
toLocaleString()Returns a string value version of the current number in a format that may vary according to a browser's locale settings.
toPrecision()Defines how many total digits (including digits to the left and right of the decimal) to display of a number.
toString()Returns the string representation of the number's value.
valueOf()Returns the number's value.

Share this article on