JavaScript reference: numbers

All information about JavaScript Number properties and methods

This article explains how to useNumberBuilt-in objects, and list their properties and methods.

A kindnumberValues can be generated using numeric literal syntax:

const age = 36
typeof age //number

Or useNumberGlobal functions:

const age = Number(36)
typeof age //number

If we addnewKeywords, we get aNumberIn return:

const age = new Number(36)
typeof age //object

Its behavior is the same asnumberTypes of. You can get the originalnumberValue usevalueOf()method:

const age = new Number(36)
typeof age //object
age.valueOf() //36

characteristic

  • EPSILONMinimum interval between two numbers
  • MAX_SAFE_INTEGERThe largest integer value that JavaScript can represent
  • MAX_VALUEThe largest positive value that JavaScript can represent
  • MIN_SAFE_INTEGERThe smallest integer value that JavaScript can represent
  • MIN_VALUEThe smallest positive value that JavaScript can represent
  • NaNSpecial value representing "not a number"
  • NEGATIVE_INFINITYSpecial value representing negative infinity
  • POSITIVE_INFINITYSpecial value representing positive infinity

These attributes are evaluated to the values listed below:

Number.EPSILON
Number.MAX_SAFE_INTEGER
Number.MAX_VALUE
Number.MIN_SAFE_INTEGER
Number.MIN_VALUE
Number.NaN
Number.NEGATIVE_INFINITY
Number.POSITIVE_INFINITY
2.220446049250313e-16
9007199254740991
1.7976931348623157e+308
-9007199254740991
5e-324
NaN
-Infinity
Infinity

Object method

We can call those methods that pass values:

I mentioned "safe integers". It is also located above, with MAX_SAFE_INTEGER and MIN_SAFE_INTEGER attributes. What is a safe integer? It is an integer that can be accurately represented as an IEEE-754 double-precision number (all integers from (2^53-1) to -(2^53-1)). Outside this range, JavaScript cannot correctly represent integers. Beyond the scope of the course, butThis is a good explanation.

Instance method

When you usenewKeyword to instantiate a value using the Number() function, we get aNumberIn return:

const age = new Number(36)
typeof age //object

This object provides some unique methods that can be used. Usually the number is converted into a specific format.

  • .toExponential(): Returns a string representing a number in exponential representation
  • .toFixed(): Returns a string that represents a number in fixed-point notation
  • .toLocaleString(): Returns a string of local specific conventions with numbers
  • .toPrecision(): Return a string representing a number, reaching the specified precision
  • .toString(): Returns a string representing the specified object in the specified radix (radix). Override the Object.prototype.toString() method
  • .valueOf(): Returns the original numeric value of the object

Download mine for freeJavaScript beginner's manual


More js tutorials: