DEV Community

Cover image for Number in JavaScript

Posted on

Number in JavaScript


  • Number is a primitive wrapper object used to represent and manipulate numbers like 12 or -3.34.
  • The Number constructor contains constants and methods which are useful for working numbers.
  • Number() function is used for converting any other type of value into numbers.
  • JavaScript Number type is a double-precision 64-bit binary format IEEE 754 value.
  • JavaScript Number can represent fractional values. But, with limitations. It can only keep about 17 decimal places of precision.
  • A number literal like 12 in JavaScript code is still floating-point value, not an integer. JavaScript has now BigInt type, but it was not designed to replace the Number but for safely store and operate on large integer even beyond the safe integer limit for Number.
  • When you use as a function, Number(value) converts a string or any other value to the Number type. If the value can't be converted, it returns NaN.

Constructor in Number

  • Using Number() you can create a new Number value.

Static properties of Number


  • The smallest interval between two representable numbers.
  • It uses to represent the difference between 1 and the smallest floating-point number greater than 1.
  • It return the smallest positive number approaching zero.


  • The maximum safe integer in JavaScript (253 - 1).


  • The largest positive representable number.


  • The minimum safe integer in JavaScript (-(253 - 1)).


  • The smallest positive representable number—that is, the positive number closest to zero (without actually being zero).


  • Special "Not a Number" value.


  • Special value representing negative infinity. Returned on overflow.


  • Special value representing infinity. Returned on overflow.


  • Allows the addition of properties to the Number object.


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

Static methods of Number


  • Determine whether the passed value is NaN.


  • Determine whether the passed value is a finite number.


  • Determine whether the passed value is an integer.


  • Determine whether the passed value is a safe integer (number between -(253 - 1) and (253 - 1)).


  • This is the same as the global parseFloat() function.

Number.parseInt(string, [radix])

  • This is the same as the global parseInt() function.


Number.isNaN(NaN)         // true
Number.isFinite(12)       // true
Number.isInteger(-32)     // true
Number.isSafeInteger(23)  // true
Number.parseFloat('50')   // 50
Number.parseInt('string') // NaN

Instance methods of Number


  • Returns a string representing the number in exponential notation.
  • toExponential() argument must be between 0 and 100.


  • Returns a string representing the number in fixed-point notation.
  • toFixed() argument must be between 0 and 100.

Number.prototype.toLocaleString([locales [, options]])

  • Returns a string with a language sensitive representation of this number. Overrides the Object.prototype.toLocaleString() method.


  • Returns a string representing the number to a specified precision in fixed-point or exponential notation.
  • toPrecision() argument must be between 0 and 100.


  • Returns a string representing the specified object in the specified radix ("base"). Overrides the Object.prototype.toString() method.
  • toString() radix argument must be between 2 and 36.


  • Returns the primitive value of the specified object. Overrides the Object.prototype.valueOf() method.


var x = 10;
x.toExponential(4) // 1.0000e+1
x.toFixed(2)       // 10.00
x.toLocaleString() // 10
x.toPrecision(7)   // 10.00000
x.toString(2)      // 1010
x.valueOf()        // 10


Number(new Date('January 03, 2005 04:34:56'))   // 1104707096000
Number('123')                                   // 123
Number('12.3')                                  // 12.3
Number('12.00')                                 // 12
Number('123e-1')                                // 12.3
Number('')                                      // 0
Number(null)                                    // 0
Number('0x11')                                  // 17
Number('0b11')                                  // 3
Number('0o11')                                  // 9
Number('foo')                                   // NaN
Number('100a')                                  // NaN
Number('-Infinity')                             //-Infinity

Top comments (0)