Number object

Objectives

  • Convert strings into numbers
  • Convert numbers into strings
  • Use methods of the JavaScript Number object
  • Use built-in Number object constants

Converting between strings and numbers

  • parseInt(string): looks for a valid integer starting at the beginning of the string and returns it as a number if found, or NaN if none is found; ignores the string past the first valid integer
  • parseInt(string, radix): looks for a valid integer in base radix starting at the beginning of the string and returns it as a number if found, or NaN if none is found; ignores the string past the first valid integer
  • parseFloat(string): looks for a valid floating point number starting at the beginning of the string and returns it as a number if found, or NaN if none is found; ignores the string past the first valid floating-point number
  • .toString(): returns the string representation of the numeric value contained in the number object it is called on
  • .toString(radix): returns the string representation of the numeric value contained in the number object it is called on; the number should be in base radix, but there is no guarantee
  • .toExponential(n): returns a representation of the number object in exponential format using n digits after the decimal
  • .toFixed(n): returns a representation of the number object using n digits after the decimal
  • .toPrecision(n): returns a representation of the number object using n digits of significance
  • .valueOf(): returns the primitive numeric value of the number object
  • you can convert a string to a number by prepending a + sign
  • you can convert a string to a number by multiplying by 1

Number constants

  • Number.EPSILON: the smallest interval between two representable numbers
  • Number.MIN_SAFE_INTEGER: the smallest safe integer
  • Number.MAX_SAFE_INTEGER: the largest safe integer
  • Number.MAX_VALUE: the largest positive number JavaScript can represent
  • Number.MIN_VALUE: the smallest positive number JavaScript can represent
  • Number.POSITIVE_INFINITY: positive infinity (occurs on overflow)
  • Number.NEGATIVE_INFINITY: negative infinity (occurs on overflow)
  • Number.NaN: special value representing "not a number"
  • Number.NEGATIVE_INFINITY: special value representing negative infinity
  • Number.POSITIVE_INFINITY: special value representing positive infinity

Useful methods

  • isNaN: determines if a value is NaN
  • isFinite: determines if a value is a finite number
  • isInteger: determines if a value is an integer
  • isSafeInteger: determines if a value is an integer in the safe integer range
  • parseFloat: converts text to a float
  • parseInt: converts text to an integer, can specify radix
  • toExponential: returns a string representation in exponential form
  • toFixed: returns a string representation in fixed-point form
  • toPrecision: returns a string representation with the specified precision
  • toString: returns a string representation, can specify radix
  • toValue: returns the primitive value