WHAT'S NEW?
Loading...

Number properties, Maxvalue, Minvalue, NaN in javascript with example - Tech-n-Savvy Blogger


The Number object represents numerical date, either integers or floating-point numbers. In general, you do not need to worry about Number objects because the browser automatically converts number literals to instances of the number class.

Syntax

The syntax for creating a number object is as follows:


var val = new Number(number);

In the place of number, if you provide any non-number argument, then the argument cannot be converted into a number, it returns NaN (Not-a-Number).

Number Properties

Here is a list of each property and their description.

Property
Description







MAX_VALUE
The largest possible  value a number in  JavaScript


can have 1.7976931348623157E+308




MIN_VALUE
The smallest possible value a number in JavaScript


can have 5E-324




NaN
Equal to a value that is not a number.




NEGATIVE_INFINITY
A value that is less than MIN_VALUE.




POSITIVE_INFINITY
A value that is greater than MAX_VALUE




prototype
A  static  property  of  the  Number  object.  Use  the


prototype  property  to  assign  new  properties  and


methods  to  the  Number  object  in  the  current


document




constructor
Returns  the  function  that  created  this  object's


instance. By default this is the Number object.








In the following sections, we will take a few examples to demonstrate the properties of Number.

MAX_VALUE

The Number.MAX_VALUE property belongs to the static Number object. It represents constants for the largest possible positive numbers that JavaScript can work with.

The actual value of this constant is 1.7976931348623157 x 10308.

Syntax

The syntax to use MAX_VALUE is:



var val = Number.MAX_VALUE;

Example

Try the following example to learn how to use MAX_VALUE.



<html>

<head>

<script type="text/javascript">

<!--

function showValue()

{

var val = Number.MAX_VALUE;


document.write ("Value of Number.MAX_VALUE : " + val );


}

//-->

</script>

</head>

<body>

<p>Click the following to see the result:</p> <form>

<input type="button" value="Click Me" onclick="showValue();" />


</form>

</body>

</html>

Output


Click the following to see the result:

Click Me



Value of Number.MAX_VALUE : 1.7976931348623157 x 10308


MIN_VALUE

The Number.MIN_VALUE property belongs to the static Number object. It represents constants for the smallest possible positive numbers that JavaScript can work with.

The actual value of this constant is 5 x 10-324.

Syntax

The syntax to use MIN_VALUE is:


var val = Number.MIN_VALUE;

Example

Try the following example.



<html>

<head>

<script type="text/javascript">

<!--

function showValue()

{

var val = Number.MIN_VALUE;

alert("Value of Number.MIN_VALUE : " + val );


}

//-->



</script>

</head>

<body>

<p>Click the following to see the result:</p> <form>

<input type="button" value="Click Me" onclick="showValue();" />

</form>

</body>

</html>

Output


Click the following to see the result:

Click Me


Value of Number.MIN_VALUE : 5e-324


NaN

Unquoted literal constant NaN is a special value representing Not-a-Number. Since NaN always compares unequal to any number, including NaN, it is usually used to indicate an error condition for a function that should return a valid number.

Note: Use the isNaN() global function to see if a value is an NaN value.

Syntax

The syntax to use NaN is:


var val = Number.NaN;


Example

Try the following example to learn how to use NaN.



<html>



<head>

<script type="text/javascript">

<!--

function showValue()

{

var dayOfMonth = 50;

if (dayOfMonth < 1 || dayOfMonth > 31)

{

dayOfMonth = Number.NaN

alert("Day of Month must be between 1 and 31.")

}

Document.write("Value of dayOfMonth : " + dayOfMonth );


}

//-->

</script>

</head>

<body>

<p>Click the following to see the result:</p> <form>

<input type="button" value="Click Me" onclick="showValue();" />

</form>

</body>

</html>


Output


Click the following to see the result:

Click Me
  

Day of the Month must be between 1 and 31.

0 comments:

Post a Comment