Javascript numbers reference

Javascript build-in Number object reference.

Create numbers:

To create an object: new Number([param])
You uses the new keyword to create an object that contains a primitive number.
To use as a function: Number([param])
When the Number object is used as a function the return will be a primitive number.
param: The param argument is optional. If it is present it can be one of the following:
  1. Any number.
  2. A string containing any number. If the string does not contain a number then the return will be NaN (Not a Number).
  3. A date that will be converted to millisecond related to 1.jan.1970 UTC.
  4. A boolean that will be converted to number 1 for true and 0 for false
An example of using the new Number(string):
<script type="text/javascript">
  // Create a number object
  var num = new Number("78.908");
  document.write("The value of num: "+num+"<br>");
 </script>

General information about the object member types:

  1. Prototype created methods, properties or constants can only be used on instances of an object or on a primitive datatype.
  2. Constructor created methods, properties or constants can NOT be used on instances of an object or on a primitive datatype.

Prototype Methods:

Syntax: toExponential(fractionDigits)
The method will return the number in an exponential form with a digit before the comma and "fractionDigits" digits after the comma.

The fractional part of the number is rounded upward.

Return value: A string.
fractionDigits: The number of digits (0 - 20) that you want after the decimal point.

If the argument is omitted, then a number of decimal places as necessary will be used.

An example of using the toExponential() :
<script type="text/javascript">
  // Create a number object
  var num = new Number(237.67599);
  document.write("Used toExponential with 2 decimals: "+
                  num.toExponential(2)+"<br>");
 </script>

Prototype Properties:

Syntax: Number.prototype.constructor
This property of the Number.prototype holds the reference back to the Number object.

You can find all the Number constants through this constructor. (Number.prototype.constructor.MAX_VALUE is the same as Number.MAX_VALUE)

Important note: This is the same as the constructor property of an instantiated object

Constructor (Number) Properties:

Syntax: Number.constructor
The constructor property is a reference to the function that will be invoked to create a Number object.

Constructor (Number) Constants:

Number.MAX_VALUE: Largest representable number. (1.7976931348623157e+308)
Number.MIN_VALUE: Smallest (closest to zero) representable number. (5e-324)
Number.NaN: Special Not-a-Number value. (NaN)
Number.POSITIVE_INFINITY: Special value to represent infinity. (Infinity)
Number.NEGATIVE_INFINITY: Special value to represent negative infinity. (-Infinity)

© 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.