Using Javascript numbers

How to use Javascript numbers.

  • In Javascript all numbers are represented as floating-point with values from ±5 x 10-324 to ±1.7976931348623157 x 10+308.
    var i=1; // integer
    var f=3.2345; // float
  • Usually you write number values in base-10, but you can express values as hexadecimal (base-16) values or as octal (base-8) as well.
    // A hexadecimal literal begins with "0x" or "0X"
    // followed by a sequence of hexadecimal digits:
    // 0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F
    var h=0xf0;  // 15*16 + 0 = 240 (base 10)
    var h=0xfff;  // 15*16*16 + 15*16 + 15 = 4095 (base 10)
    // A octal literal begins with "0"
    // followed by a sequence of octal digits:
    // 0,1,2,3,4,5,6,7
    var h=070;  // 7*8 + 0 = 56 (base 10)
  • Floating-point is represented as the integral part of the number, followed by a decimal point and the fractional part of the number.
    var x=.333333333333333333;
    var y=6789.3476;
    
  • You can also express the floating-point values with exponential notation. This is done by putting the character e (or E) after the decimal digits, followed by an optional plus or a minus sign, followed by an integer exponent.
    // The full floating value syntax is then:
    // [digits][.digits][(E|e)[(+|-)]digits]
    var f=32.12345E-32;  // 32.12345 * 10-32
    var z=31.567e23;     // 31.567 * 1023
  • Javascript have some global numeric constants:
    Constant Meaning
    Infinity Special value to represent infinity. When a floating-point value becomes larger than the largest representable finite number.
    NaN Special Not-a-Number value. (when a mathematical operation (such as division of zero by zero) yields an undefined result or an error.)

© 2010 by Finnesand Data. All rights reserved.
This site aims to provide FREE programming training and technics.
Finnesand Data as site owner gives no warranty for the correctness in the pages or source codes.
The risk of using this web-site pages or any program codes from this website is entirely at the individual user.