Skip to content

Latest commit

 

History

History
59 lines (38 loc) · 3.26 KB

File metadata and controls

59 lines (38 loc) · 3.26 KB

The Number Object

JavaScript has only one type of number.

Numbers can be written with, or without, decimals.

Extra large or extra small numbers can be written with scientific (exponent) notation:

123e5 // 12300000

The number is a basic data type in JavaScript. JavaScript also supports Number objects. The object is the original value of the package object. When necessary, JavaScript automatically converts between the original data and the objects. You can explicitly create a Number object with the constructor Number(). Although it is not necessary to do so. Usage:

var num=new Number(value);

Parameter value is the value of the Number object to be created or the value to be converted into a numeric value.

Constructor Number() can be used without operator new and directly as a transformation function to use. In this way, when the Number is called, it transforms itself into a number and then returns the converted value (or NaN). Usage:

var num=Number(value);

Number object has two generic object properties: constructor and prototype. all the objects in JS have these two properties. They are 2 very important attributes. Because of their importance and complexity, we will learn it in the future.

properties of the Number object

MAX_VALUE: The maximum number that can be expressed in JS. Usage: Number.MAX_VALUE. Its approximate value is 1.7976931348623157e+308

MIN_VALUE: The minimum number that can be expressed in JS(Close to 0, but not negative). Usage: Number.MIN_VALUE. Its approximate value is 5e-324

NaN: Non numeric value. Abbreviations of "Not a Number". When some arithmetic operations (such as the square root of a negative number) or the result of the method are not numbers, return NaN. Usage: Number.NaN. It can be simplified and replaced with NaN.

Please note: the results of comparison between NaN and other values are always not equal(including its own). Therefore, can not be compared with Number.NaN to detect a value is not a number but can only call isNaN() to compare.

NEGATIVE_INFINITY: The value represents the negative infinity. Usage: Number.NEGATIVE_INFINITY. When a number is generated in an arithmetic operation or function and it smaller than -Number.MAX_VALUE return this value. It can be simplified and replaced with -Infinity.

POSITIVE_INFINITY: The value represents the positive infinity. Usage: Number.POSITIVE_INFINITY. When a number is generated in an arithmetic operation or function and it larger than Number.MAX_VALUE return this value. It can be simplified and replaced with Infinity.

sources:

How to test for it

const whatNumberIsIt = (n) => {
  switch(true){
      case (n == Number.MAX_VALUE): return "Input number is Number.MAX_VALUE";
      case (n == Number.MIN_VALUE): return "Input number is Number.MIN_VALUE";
      case (isNaN(n)): return "Input number is Number.NaN";
      case (n == Number.NEGATIVE_INFINITY): return "Input number is Number.NEGATIVE_INFINITY";
      case (n == Number.POSITIVE_INFINITY): return "Input number is Number.POSITIVE_INFINITY";
      default: return `Input number is ${n}`;
  };
};