Home > DeveloperSection > Articles > JavaScript Number Object

JavaScript Number Object

JavaScript JavaScript 
0 Comment(s)
 2321  View(s)
Rate this:

JavaScript Number Object


In this article I am going to explain about number objects in Java Script. Actually number objects consists

of numeric values. It can be either floating point values or integer values.

In Java Script numeric objects are created with the help of new Number(). If the value parameter cannot

 be converted to a number then it will return NaN which means Not-a -Number.

Syntax :

Var num=new Number(value);

There are various methods and properties associated with the number objects they are as follows:

Properties of Number objects:

1.       MAX VALUE : It will return the largest number possible in Java Script.

2.       MIN VALUE : It will return the smallest number possible in Java Script.

3.       NaN:  It represents Not-a-Number value.

4.       Prototype : allows us to add properties and methods to an object.

5.       Negative Infinity : It is a value which is less than MIN_VALUE.

6.       Possitive Infinitive : It is a value that is greater than MAX_VALUE.

Mehods of Number objects:

1.       Constructor : Returns the function that created this object’s instance, by default this is the number object.

2.       toExponential(): Forces a number to display in the Exponential format, even if the number is in the format

which can be normally displayed in Java Script without converting it to exponential format.

3.       toFixed():Formats  a number with a specific number of digits to right of the decimal.

4.       toString() : Converts a number object to string representation of a number.

5.       valueOf() : Returns the primitive value of a number object.

6.       toPrecision() : It defines how many total digits of a number to display.





Using properties of Java Script Number objects:

var n1= Number.MAX_VALUE;

var n2= Number.MIN_VALUE;

var n3= Number.NaN;

var n4= Number.Negative_Infinity;

var n5= Number.Positive_Infinity;




Using Methods of Number objects:

var number=75;

var exponent=number. toExponential(2):

var  fixed=number.toFixed(3);

var precision=number.toPrecision(4);

var string=number.toString();

var val=number.valueOf();



Example of Java Script Number Objects and Properties:


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">


    <script type="text/javascript">

        // ******************Methods of a number object ***********************//

        var number = 75;

        var exponent = number.toExponential(2);

        var fixed = number.toFixed(3);

        var precision = number.toPrecision(4);

        var string = number.toString();

        var val = number.valueOf();

        document.write("Exponent format of a number is " + exponent + "<br/>");

        document.write("After fixing to 3 places the number becomes" + fixed + "<br/>");

        document.write("Precision " + precision + "<br/>");

        document.write("String Representation of a number is " + string + "<br/>");

        document.write("Primitive Value is " + val + "<br/>");


        // ****************** Properties of a number object ********************//


        var n1 = Number.MAX_VALUE;

        var n2 = Number.MIN_VALUE;

        var n3 = Number.NaN;

        var n4 = Number.Negative_Infinity;

        var n5 = Number.Positive_Infinity;

        document.write("Maximum value of a number is " + n1 + "<br/>");

        document.write("Minimum value of a number is " + n2 + "<br/>");

        document.write("Nan Value " + n3 + "<br/>");

        document.write("Negative Infinity " + n4 + "<br/>");

        document.write("Positive Infinity " + n5 + "<br/>");







Output is:

JavaScript Number Object





Through this article we came to know about Number objects in Java Script and how to use it, it is important

 part in Java Script because we have to perform various computations for that we must know about Java Script Number Objects.

Don't want to miss updates? Please click the below button!

Follow MindStick