Sunday, February 16, 2020

How to implement the various JavaScript Number properties with examples


Hello guys! good morning to you all and welcome to my new tutorial on JavaScript. I want to use this medium to wish you all a happy Valentine's Day. In my last tutorial post we discussed about Learning JavaScript Number Object with examples, so in this tutorial we will be using few examples to demonstrate the properties of Number.

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

You can try the following example to understand and learn how to implement 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 = "ClickMe"  on click = "showValue();"  />      
          </form>
     </body>
</html>

Output

Below is the output of the above example

Click the following to see the result 

 

You can also read our tutorial post on: JavaScript if...else Statement

Number - 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.

This constant has actual value of 5 x 10-324

Syntax

The syntax to use MIN_VALUE is -

var val = Number.MIN_VALUE;

Example

You can try the following example to understand and learn how to implement MIN_VALUE.

<html>
     <head>
          <script  type = "text/Javascript">
               <!--
                    function  showValue()  {
                         var  val = Number.MIN_VALUE;
                         document.write("Value of Number.MIN_VALUE : " + val );     
                    }
               //-->
          </script>
     </head>

     <body> 
          <p>Click the following to see the result</p>
          <form>
               <input type = "button"  value = "ClickMe"  on click = "showValue();"  />      
          </form>
     </body>
</html>

Output

Below is the output of the above example

Click the following to see the result 

 

You can also read our tutorial post on: JavaScript - Switch Case


Number - 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

You can try the following example to understand and learn how to implement NAN.

<html>
     <head>
          <script  type = "text/Javascript">
               <!--
                    function  showValue()  {
                         var dayOfMonth = 40;

                         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 = "ClickMe"  on click = "showValue();"  />      
          </form>
     </body>
</html>

Output

Below is the output of the above example 

Click the following to see the result 

 

You can also read our tutorial post on: JavaScript - Loop Control


Number - NEGATIVE_INFINITY

This is a special numeric value representing a value less than Number.MIN_VALUE. This value is represented as an "-infinity" number. It resembles an infinity number in its mathematical form or behavior. For example, any thing or any number multiplied by NEGATIVE_INFINITY is NEGATIVE_INFINITY, and any thing divided by NEGATIVE_INFINITY is zero.

Because NEGATIVE_INFINITY is a constant, it is a read-only property of Number.

Syntax

Below is the basic syntax to use NEGATIVE_INFINITY in your program -

var val = Number.NEGATIVE_INFINITY;

Example

You can try the following example below to understand and also learn how to use NEGATIVE_INFINITY.

<html>
     <head>
          <script  type = "text/Javascript">
               <!--
                    function  showValue()  {
                         var smallNumber = ( - Number.MAX_VALUE ) * 2
                         if  (smallNumber == Number.NEGATIVE_INFINITY) {
                              alert("value of smallNumber : " + smallNumber);
                         }
                    }
               //-->
          </script>
     </head>

     <body> 
          <p>Click the following to see the result</p>
          <form>
               <input type = "button"  value = "ClickMe"  on click = "showValue();"  />      
          </form>
     </body>
</html>

Output

Below is the output of the above example

Click the following to see the result 

 

Number - POSITIVE _INFINITY

This is a special numeric value representing a value greater than Number.MAX_VALUE. This value is represented as "infinity". It resembles an infinity in its mathematical behavior. For example, any thing multiplied by POSITIVE_INFINITY is POSITIVE_INFINITY, and any thing divided by POSITIVE_INFINITY is zero.

Because POSITIVE_INFINITY is a constant, it is a read-only property of Number.

Syntax

Below is the basic syntax to use POSITIVE_INFINITY in your program -

var val = Number.POSITIVE_INFINITY;

Example

You can try the following example below to understand and also learn how to use POSITIVE_INFINITY in your program.

<html>
     <head>
          <script  type = "text/Javascript">
               <!--
                    function  showValue()  {
                         var bigNumber = Number.MAX_VALUE * 2
                         if  (bigNumber == Number.POSITIVE_INFINITY) {
                              alert("value of bigNumber : " + bigNumber);
                         }
                    }
               //-->
          </script>
     </head>

     <body> 
          <p>Click the following to see the result</p>
          <form>
               <input type = "button"  value = "ClickMe"  on click = "showValue();"  />      
          </form>
     </body>
</html>

Output

Below is the output of the above example

Click the following to see the result 

 

You can also read our tutorial post on: Css Pseudo Classes


Object - prototype
The prototype property allows you to add properties and methods to any object(Number, Boolean, String and Data etc.).

Note - Prototype is a global property which is available with almost all the objects.

Syntax
It's syntax is as follows -

object.prototype.name = value

Example
You can try the following example below to understand and learn how to implement Object prototype.

<html>
     <head>
          <title>User-defined objects</title>
          <script  type = "text/Javascript">
               function  book(title,  author)  {
                    this.title = title;
                    this.author = author; 
               }
          </script>
     </head>

     <body>
          <script  type = "text/Javascript">
               var  myBook = new book("Java" , "Kennedy");         
               book.prototype.price = null; 
               myBook.price = 300;

               document.write("Book title is : " + myBook.title + "<br />");       
               document.write("Book author is : " + myBook.author + "<br />");      
               document.write("Book price is : " + myBook.price + "<br />");
          </script> 
     </body>
</html>

Output
Below is the output of the above example

Book title is : Java
Book author is : Kennedy
Book price is : 300


Number - constructor()
It returns a reference to the  Number function that created the instance's prototype.

Syntax
It's syntax is as follows -

number.constructor()

Example
You can try the following example below to understand and learn how to implement Number constructor()

<html>
     <head>
          <title>JavaScript constructor() Method</title>
     </head>

     <body>
          <script  type = "text/Javascript">
               var  num = new Number( 125.1234 );    
               document.write("num.constructor() is : " + num.constructor );       
          </script> 
     </body>
</html>

Output
Below is the output of the above example 

num.constructor() is : function Number()  { [native code] }    

Alright guys we have come to the end of this tutorial post, feel free to ask your questions via the comment box. In my next tutorial post, we will be looking into JavaScript Number Methods with series of examples.

Follow us on our various social media handles to stay updated with our latest tutorials, thanks for reading and bye for now.
Share:

0 comments:

Post a Comment

Hello dear readers! Please kindly try your best to make sure your comments comply with our comment policy guidelines. You can visit our comment policy page to view these guidelines which are clearly stated. Thank you.