Numbers
Constructor
Number() - simple call converts a value into a number primitive
- conversion works for various bases
- "+" & "-" - plus and minus signs are analogs, which are also used for conversion. "+" works identically as Number()
- e.g.
- +' 123' - returns 123. spaces are ignored
- +'123q' - returns NaN. non-digit characters are not ignored
Static Methods
Number.isNaN() - safer analog of global isNaN. doesn't convert to number
Number.isFinite() - safer analog of global isFinite. doesn't convert to number
Number.isInteger()
Number.isSafeInteger() - check whether number within range -(253 - 1) to 253 - 1
Methods
toString(radix?) - returns string representation of a number
- radix - integer between 2 and 36 that represents the desired base of the output string number. default is 10
toFixed(digits?) - rounds to the nearest number and converts to string
- digits - sets amount of digits after comma. zeros are added at the end if needed. default value is 0
- negative literals return number, because of operation priority: toFixed doesn't consider minus sign, later minus sign converts string to number
- e.g. -2.34.toFixed(1) === -2.3
toLocaleString() - returns a string with a language-sensitive representation of this number
Global Functions
isNaN() - is needed, because neither value equals NaN
- automatically converts to number
isFinite() - returns false for Infinity, -Infinity and NaN
- automatically converts to number
parseInt(string, radix) - is used to convert string to integer
- radix - integer between 2 and 36 that represents the base of the string number. does not always default to 10
- e.g.
- parseInt(' 123q') - returns 123. spaces are ignored
- parseInt('q123') - returns NaN. non-digit characters are only ignored when they follow after a number
parseFloat() - is used to convert string to float
Math
Math - default object providing properties and methods, which are used for mathematical operations
- unlike other default objects, Math is not a constructor, so cannot be invoked with "new". all properties and methods of Math are static
Math.random() - returns random float number from 0 to 1 (not including 1)
- Math.random() * max - random float from 0 to max (not including max)
- Math.random() * (max – min) + min - random float from min to max (not including max)
- Math.random() * (max – min + 1) + min - random float from min to max (including max)
- to get integer instead of float, result should be wrapped with Math.floor()
- e.g. Math.floor(Math.random() * 6 + 2) - random integer from 2 to 7 (including 7)
- tricks:
- array.sort(() => Math.random - 0.5) - mix array elements
Math.sqrt() - square root
Math.pow(base, exponent) - exponent
Math.abs() - module
Math.min() - minimum number out of arguments
Math.max() - maximum number out of arguments
- Math.min(...array) - array values should be spread before passing
Rounding
Math.floor() - downside
- Math.floor(5 / 2) - Euclidean division (works wrong way for negative values)
Math.ceil() - upside
Math.round() - to the nearest
Math.trunc() - remove fractional digits
- differs from "floor" when applied on negative values
BigInt
BigInt() - is used to create numeric values which are too large to be represented by the number primitive. bigint value is also primitive
- accepts integer or string
- 5432398664227717n - bigint value can also be created by appending "n" to the end of an integer literal
- bigint can't be mixed with number