• isNaN function - Number or not number - Numeric objects of type Number in Javascript. Full JavaScript Number Reference

    JavaScript provides a Number object to represent numeric data types. This class contains constants and methods that are often used when working with numbers. I'm going to tell you about using the Number object.

    The constructor in the Number class is rarely used because when numeric types are created, they are automatically converted to a Number object. But still, just in case, I give an example of using the Number object constructor:

    Var num = new Number(5.1);

    A regular number is passed as a parameter to the constructor of the Number class.

    Now let's move on to the properties of the Number class. Let's consider the constant properties MIN_VALUE and MAX_VALUE , which show the minimum possible value (greater than zero in this case) and the maximum possible value for numbers. Let's print out these constants (note that we are accessing properties of a class, not an object!):

    Document.write(Number.MIN_VALUE + "
    ");
    document.write(Number.MAX_VALUE + "
    ");

    If you run this script, you will see two values. As you can see, the precision is very high (follows from MIN_VALUE ), and the maximum value is huge (follows from MAX_VALUE ).

    The next two constants mean infinity. The POSITIVE_INFINITY constant means positive infinity, and NEGATIVE_INFINITY means negative infinity:

    Document.write(Number.POSITIVE_INFINITY + "
    ");
    document.write(Number.NEGATIVE_INFINITY + "
    ");

    How can you get infinity in calculations? And it’s very simple, for example, to calculate the exponent of 1000:

    Document.write(Math.exp(1000));

    As a result, you will see: "Infinity". The peculiarity of infinity is that it can participate in mathematical operations. For example:

    Document.write(1000 / Math.exp(1000));

    As a result you will see 0. And, indeed, if 1000 is divided by an infinitely large number, it will be 0.

    Now let's move on to the methods of the Number object. Let's take a quick look at an insanely commonly used method called Number(). This method converts a variable (which is passed as a parameter) into a number. Obviously, this method is used primarily when converting strings to numbers. Here's an example:

    Var num = Number("321.5");

    Please note that we are passing a string, but the number “321.5” is returned. Specifically when we accept values ​​from the user from the prompt() function. Then we get a line! This is very important; if you do not understand this, you will not be able to avoid mistakes. And for this string to become a number, it must be passed through the Number() method. Therefore, before using the data received from the user as numbers, it is necessary to convert them into these same numbers. This is a very important rule!

    And the last method that is used quite often is toFixed() . This method is applied to an object. The toFixed() method is used to round fractional numbers. As parameters, this method takes a number indicating the number of decimal places we want to leave:

    Var num = 931.1256;
    document.write(num.toFixed(3));

    As a result, you will see: “931.126”, that is, a number rounded to three decimal places.

    These are all the properties and methods of the Number object that you will regularly use in your practical activities.

    Last update: 11/1/2015

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

    Var x = new Number(34); var y = new Number("34"); document.write(x+y); // 68

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

    However, you can create a Number object by simply 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 number. Approximately equal to 1.79E+308. Numbers greater than this value are treated as Infinity

      Number.MIN_VALUE: The smallest possible positive number. 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 represents negative uncertainty that occurs when overflow occurs. For example, if we add two negative numbers that modulo are equal to 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. Just like negative uncertainty, it arises when there is 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 whether 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

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

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


      The isNaN function returns only two values: either True - true or False - false .

      If the variable does not contain a number, then the isNaN function will return True - you should pay special attention to this (NOT A NUMBER is TRUE. isNaN translates as not a number). If the variable contains a number, then it will return False.

      var name = "Anton" ; /* string object */
      var age = 35 ; /* numeric object */

      if(isNaN (name )) /* If the name variable is not a number, this is True */
      {
      document.write(name + " is not a number")
      }
      else /* otherwise - False */
      {
      document.write(name + " is a number")
      }

      Remember that isNaN is a built-in javascript function.

      , thanks to which, and using the isNaN function, a check was carried out: whether the value of the variable is a number or not.

      In addition, here you will need to remember the topic of arrays and loops and functions in javascript.

      var randSimbol = ["Milk" , 77 , 11 , "Honey" , - 88 ];

      function number (randSimbol)
      {
      for(i = 0 ; i< randSimbol .length ; i ++ ) /* Составляем цикл для перебора всех элементов масива */
      {
      if(isNaN (randSimbol )) /* If the array element is not a number (it is True), then... */
      {
      document.write(randSimbol + " is not a number

      " )
      }
      else /* Otherwise, the array element is a number (and this is False).... */
      {
      document.write(randSimbol + " is a number

      " )
      }
      }
      }

      number(randSimbol); /* Call the function */

      This is how the isNaN function works when applied to numeric Number objects in Javascript.

      Hello! In this tutorial, we continue the theme of built-in objects in JavaScript and after the Date object, we will look at the built-in object Number, which represents numbers. To create a number, you simply pass a number to Number or a sink representing the number:

      Var num1 = new Number(55); var num2 = new Number("55"); document.write(num1+num2); //110

      The definitions of the variables num1 and num2 will be similar.

      Creating a Number object

      You can create a Number object by assigning a specific number to a variable:

      Var a1 = 32;

      The Number object has several properties and methods. Here they are:

      • Number.MAX_VALUE: Returns the largest integer that is 1.79E+308. All other numbers that will be greater than this value are Infinity, that is, infinity
      • Number.MIN_VALUE: Returns the smallest positive integer that is 5e-324
      • Number.NaN: indicates that the object is not a number
      • Number.NEGATIVE_INFINITY: Indicates the negative uncertainty that occurs when overflowing. For example: var x1 = -10 * Number.MAX_VALUE var y1 = -10 * Number.MAX_VALUE var z1 = x1 + y1; if(z1===Number.NEGATIVE_INFINITY) document.write("there is negative uncertainty"); else document.write(z1);
      • Number.POSITIVE_INFINITY: correspondingly positive uncertainty. Can occur when overflowing only this time in a positive direction: var x1 = Number.MAX_VALUE var y1 = Number.MAX_VALUE var z1 = x1 * y1; if(z1===Number.POSITIVE_INFINITY) document.write("there is positive uncertainty"); else document.write(z1);

      Let's list some of the main methods of the Number object:

      • isNaN(): checks whether the object is a number, if the object is not a number, then returns true: var a1 = Number.isNaN(Number.NaN); //true var b1 = Number.isNaN(true); //false - new Number(true) = 1 var c1 = Number.isNaN(null); //false - new Number(null) = 0 var d1 = Number.isNaN(255); //false var e1 = Number.isNaN("55"); //false

        But this expression will return false, despite the fact that the value is not a number:

        Var fal = Number.isNaN("hello world"); //false

        Therefore, to avoid such situations, you should use the global isNaN function:

        Var fal = isNaN("hello world"); //true

      • parseFloat(): This function converts a string to a number, separated by a comma. For example: var a1 = Number.parseFloat("44.90"); //44.9 alert(a1); var b1 = Number.parseFloat("hello world"); //NaN alert(b1); var c1 = Number.parseFloat("34hello"); //34 alert(c1);
      • parseInt(): This function converts a string to an integer. Here's an example: var a1 = Number.parseInt("44.90"); //44 document.write(a1); var b1 = Number.parseInt("hello world"); //NaN document.write(b1); var c1 = Number.parseInt("25hello"); //25 document.write(c1);
      • toFixed(): allows you to keep a floating point number at a specified number of decimal places. Example: var a1 = 10/1.44; document.write("Before toFixed() method: " + a1 + "
        "); a1 = a1.toFixed(2); // 2 decimal places remain document.write("After the toFixed() method: " + a1 + "
        "); In the browser we get: Before the toFixed() method: 6.944444444444445 After the toFixed() method: 6.94
      Results

      The Number object is used to create numbers

      The isNaN() method checks whether an object is a number and returns true if it is not.

      If the number is outside the JavaScript numeric range, then the special value Infinity is returned

      To convert a string to a number, use the parseInt/parseFloat functions

      The toFixed() method allows you to round a number to the required number of decimal places.

      A Number object represents a numeric date, either an integer or a floating point number. In general, you don't 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:

      Var val = new Number(number);

      If in place of a number you provide any non-number arguments, then the argument cannot be converted to a number, the function returns NaN (Not-A-Number).

      Number properties

      Here is a list of all properties and their descriptions.

      In the following sections, we will provide several examples to demonstrate the properties of Number.

      Number Methods

      The Number object contains only the standard methods that are part of the definition of each object.

      Sr.No Method & Description
      1 – Force series to be displayed in scientific notation even if the number is in the range in which JavaScript typically uses standard notation.
      2 – Converts a number with a specified number of digits to the right of the decimal point.
      3 – Returns a version of the string value of the current number in a format that may vary according to the local browser settings.
      4