JavaScript Number object

The Number object represents numbers. To create a number, you must pass a number or a string representing the number to the Number constructor:

var x = newNumber(34);
var y = newNumber('34');
document.write(x+y); // 68

The definitions of x and y in this case will be almost the same.

However, you can also create a Number object simply by assigning a specific number to a variable:

var z = 34;

The Number object provides a number of properties and methods. Some of its properties:

Number.MAX_VALUE: the 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 on overflow. For example, if we add two negative numbers that are modulo Number.MAX_VALUE. For example:

var x = -1 * Number.MAX_VALUE
var y = -1 * Number.MAX_VALUE
var z = x + y;
if(z===Number.NEGATIVE_INFINITY)
    document.write("negative uncertainty");
else
    document.write(z);
    

Number.POSITIVE_INFINITY: positive uncertainty. As well as negative uncertainty, it occurs during overflow, only now in a positive direction:

var x = Number.MAX_VALUE
var y = Number.MAX_VALUE
var z = x * y;
if(z===Number.POSITIVE_INFINITY)
    document.write("positive uncertainty");
else
    document.write(z);
    

Some basic methods:

isNaN(): Determines if the 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

But the following expression will return false even though the value is not a number:

var f = Number.isNaN("hello"); // false

To avoid such situations, it is better to use the global isNaN function :

var f = isNaN("hello"); // true

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(): Leaves a specified number of decimal places in a floating point number. For example:

var a = 10 / 1.44;
document.write("Before the toFixed() method: " + a + "<br/>");
a = a.toFixed(2); // leave two decimal places
document.write("After the toFixed() method: " + a + "<br/>");

Browser output:

Before the toFixed() method: 6.944444444444445
After the toFixed() method: 6.94