Integers:
JS integers are accurate up to 15 digits.
Floats:
In JS, the decimal numbers are always stored as floating point numbers, bits 0 - 51 are the value or fraction. Bits 52-62 are the exponent, the signed bit is bit 63.
Exponents:
12e5 = 12 * 10^5 = 1 200 000
25e-5 = 25 * 10^ (-5) = 0.00025
Special JS Numbers:
- NaN: What if we have a string that's not a number? E.g. "x" / "10" = NaN (not a number)
- isNaN([variable_in_question]) is a function used to check if the variable in question is a valid number and will return false if the variable isn't. The type of NaN is still a number. Any math operations with NaN will return NaN.
- + / - Infinity: Infinity is the value given to a number out of range for JS numbers. Infinity also has type of number. (This can be assigned like so: var x = -Infinity or var x = Number.NEGATIVE_INFINITY); Positive version is var y = Infinity or var y = Number.POSITIVE_INFINITY;
-
Number.MAX_VALUE returns the largest number possible in JS (2^1024), anything higher will overflow to +Infinity.
-
Number.MIN_VALUE returns the smallest number(closest to 0) possible in JS (5^-324)
Hexadecimal: use 0x prefix like many other hexadecimal representations. E.g. 0xFF
Queries: (*functions must be called on the Number object and will return false if given any non-number type variables)
- Number.isInteger([value_in_question]) returns true if the value given is an integer, returns false on all other values such as decimals, infinities etc.
- Number.isSafeInteger([value_in_question]) returns true if the given value can be represented as IEEE 754 double precision number: -(2^53-1) to 2^53-1
- Number.isFinite([value_in_question]) returns true if the given value is finite.
Conversions:
- toString([base]) is a method used to convert numbers to strings according to the base. The default is base 10 if not given.
- valueOf() is a method that converts a number object to a primitive number. However because good programming practice discourages creating number objects with "new" this method should really never be used under regular circumstances.
- Number([variable]) is a global JS function (not a method) that converts variable given to a number if possible and returns NaN if not possible. E.g. Number(true) -> 1; Number("10.33") -> 10.33 etc. but Number("some") -> NaN. Whitespace is trimmed but commas are not recognized. This function can also convert a date to the number of miliseconds that have passed since 1970 Jan 1.
- parseInt([string], [base]) is a global JS function that takes a string and returns and integer if possible, NaN if not possible. Decimals will be truncated. If the string contains more than one number only the first number will be returned. It works with hexadecimal as well E.g. parseInt("0x10") -> 16. Base can be integers 2 - 36 but is optional.
- parseFloat([string]) is a global JS function that takes a string and returns a float if possible, NaN if not possible.
Formats:
- toExponential([digits_behind_decimal]) is a method used to format the number in exponential notation, rounded to the number of digits given. The default is unrounded and the number keeps its value but the notation will change to exponential notation. E.g. (956).toExponential() -> 9.56e+2
- toFixed([digits_behind_decimal]) is a method used to round the number to given number of places. Default is 0, round to nearest integer E.g. (96.66).toFixed(1) -> 96.7
- toPrecision([num_sig_figs]) is a method used to round the number to the given number of significant figures. E.g. (9.78).toPrecision(2) -> 9.8
Combining Numbers:
One interesting phenomenon in JS is that strings if used with math operators, will first try to be converted to numbers. The only exception is the concatenation operator +.
E.g. "10" + "10" = "1010" but "10" - "10" = 0 (notice that the result is a number) the same goes for "10" * "10 = 100 and "10" / "10" = 1