The number type may have a couple of different literal values. It may be expressed as an integer or a floating point.

The most common format is a literal decimal integer, as seen here:

`var intNumber = 100; //integer`

Further, the integer number maybe expressed as a decimal integer (as seen above) but also as an octal (base 8) or a hexadecimal (base 16) literals. The octal numbers must start with "0" while the hexadecimal ones start with "0x", as shown in the example:

`var octNumber = 0144 // 100 in decimal`

`var hexNumber = 0x64 // 100 in decimal`

The floating-point numbers are used for calculations with the decimal points but are also very handy for operating with extremely small or extremely big numbers, via the e-notation. The following example shows some floating-points possibilities:

`var floatNumber1 = 5.1234;`

`var floatNumber2 = 2.0;`

`var floatNumber3 = 99.102e5; // 9910200 (same as 99.102 * 105)`

`var floatNumber4 = 4e-6; // 0.000004`

Because floating numbers use twice as much memory, the script will convert any number with a decimal point but without numbers (except "0") behind it, into an integer (i.e. "2.0" becomes "2").

Do not use floated-point numbers for arithmetic computations (i.e. if(a + b) == 0.3) because, due rounding errors, script does not compute all numbers properly.

## Range of values

If the numbers are too big or too small for the ECMAScript float-point range, they will become Infinity or - Infinity. The minimum and maximum numbers in JS can be tested with the function Number.MIN_VALUE and Number.MAX_VALUE, respectively. The predefined function isFinite() may be used to test the number for infinity, like in this example:

`var result = Number.MAX_VALUE + Number.MAX_VALUE;`

`alert(isFinite(result)); // false`

## Not-a-Number (NaN)

If an operation intended to be performed results with a non number, including dividing by "0", the script will return a value NaN which refers to a phrase Not-a-Number.

Because any operation involving at least one NaN value, and because two NaN values are never the equal (alert(NaN == NaN); // false), the script offers a function isNaN() to test for it. The following examples show the practical usages of the function:

`alert(isNaN(NaN)); // true`

`alert(isNaN(10)); // false ? 10 is a number`

`alert(isNaN("word")); // true ? cannot be converted into a number`

`alert(isNaN(true)); // false ? it can be converted into a number (true == 1)`

## Conversions

To convert values into a number, or from one number type to another number type, functions Number(), parseInt() and parseFloat() maybe used.

Following rules of conversions apply to the Number() function:

• Boolean values true and false become "1" and "0";
• When applied to other numbers, the value is normally passed through;
• Null value returns "0";
• Undefined value returns NaN;
• If applied to strings, these rules apply:
• If the string contains only numbers, it is converted directly decimal integers (i.e. string="123", becomes number=123); leading zeros are ignored;
• If the string contains float numbers only, it will be converted directly to numbers as said above;
• If the string contains a valid hexadecimal format number (i.e. string="0x9") it will be converted into an integer that matches that number;
• If the string is empty, it is converted into "0";
• All other values will be converted into NaN;
• When applied to objects it will be converted to numbers according the rules from above, except if NaN is returned, in which case it will be converted into a string.

To achieve better precision, it is suggested to use parseInt() and parseFloat() functions. These functions recognize empty strings as NaN, will ignore letters and work only with number if the string begins with them (i.e. parseInt("123code") returns 123), and also parseInt() recognizes octal and hexadecimal numbers properly.

If the format is known in advance, the parseInt() function may take the second argument as a radix describing which format is being parsed. I.e.:

`var num = parseInt("0xAF", 16); // 175`

`var num = parseInt("AF", 16); // 0x is excluded because it's not necessary when the radix is given`

If the string contains a whole number, the parseFloat function will return an integer, instead of a floating-point.

## Example

### The exampl of number data type:

›› go to examples ››