The Number object represents numbers. To create a number, you must pass a number or a string to the Number constructor, representing a number. Another way to assign a number to a variable:
var x = new Number(33); var y = new Number('33'); document.write(x + y); // 66
Properties of the Number object
The Number object provides a number of properties and methods. Some of its properties:
- Number.MAX_VALUE – largest possible integer. Approximately equal to 1.79E+308. Numbers greater than this value are treated as Infinity
- Number.MIN_VALUE – the smallest possible positive integer. Approximately equal to 5e-324 (somewhere around zero)
- Number.NaN – a special value that indicates that the object is not a number
- Number.NEGATIVE_INFINITY – A value that denotes a negative uncertainty and that occurs when an overflow occurs.
- Number.POSITIVE_INFINITY – positive definiteness
- isNaN() – determines if an object is a number. If the object is not a number, then true is returned.
var a = Number.isNaN(Number.NaN); // true var b = Number.isNaN(true); // false - new Number(true) = 1 var c = Number.isNaN(null); // false - new Number(null) = 0 var d = Number.isNaN(25); // false var e = Number.isNaN("54"); // false
- parseFloat() – converts a string to a floating point number. For example:
var a = Number.parseFloat("34.90"); // 34.9 document.write(a); var b = Number.parseFloat("hello"); // NaN document.write(b); var c = Number.parseFloat("34hello"); // 34 document.write(c);
- parseInt() – converts a string to an integer. For example:
var a = Number.parseInt("34.90"); // 34 document.write(a); var b = Number.parseInt("hello"); // NaN document.write(b); var c = Number.parseInt("25hello"); // 25 document.write(c);
- toFixed – wraps to a coma by a certain number of characters
var a = 10 / 1.44; a = a.toFixed(2); // leave two decimal places