Number
Objectives
- convert strings into numbers
- convert numbers into strings
- use methods of the JavaScript Number object
- use built-in Number object variables
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.MAX_VALUE: the largest numeric value JavaScript can represent
- Number.MIN_VALUE: the smallest numeric value JavaScript can represent
- Number.POSITIVE_INFINITY: positive infinity (occurs on overflow)
- Number.NEGATIVE_INFINITY: negative infinity (occurs on overflow)
- Number.NaN: used to represent a non-numeric value
Useful functions
- isNaN(n): returns true if n is not a legal number
- isFinite(n); returns true iff n represents a finite numeric value