Slide navigation: Forward with space bar, →, or PgDn. Backwards with ← or PgUp.
42
, 0.42
4.2e-10
0x2A
0b101010
0o52
052
parseInt
, toString
have optional base
argument (between 2 and 36):
(1000).toString(16) // '3E8'
parseInt('3E8', 16) // '1000'
0
. -0
are considered different by a few functions (Object.is
)Infinity
, -Infinity
, NaN
Number
functions isFinite
, isNaN
isFinite
, isNaN
isNaN('Fred')
, isFinite([0])
are true
parseFloat
, parseInt
, also in Number
.Number
functions isInteger
, isSafeInteger
Number
instance methods for floating-point formatting toFixed
, toExponential
, toPrecision
:
Math.sqrt(2).toPrecision(6)
is '1.41421'
Number
constants MIN_VALUE
, MAX_VALUE
, MIN_SAFE_INTEGER
, MAX_SAFE_INTEGER
, EPSILON
NaN
, POSITIVE_INFINITY
, NEGATIVE_INFINITY
NaN
, Infinity
, -Infinity
Math
constants E
, PI
, SQRT2
, SQRT1_2
,LN2
, LN10
, LOG2E
, LOG10E
abs , sign , min , max |
min and max any number of arguments |
random |
Random number 0 ≤ r < 1 |
round , trunc , floor , ceil |
Applied to -3.9 yield -4, -3, -4, -3 Also fround , ftrunc , ffloor , fceil |
pow , exp , expm1 , log , log2 , log10 , log1p , |
`x^y`, `e^x`, `e^x-1`, `log x`, `log_2 x`, `log_10 x`, `log(x + 1)` |
sqrt , cbrt , hypot |
`sqrt x`, `root{3} x`, `sqrt(x^2+y^2)` |
sin , cos , tan , asin , acos , atan , atan2 |
Trigonometric functions |
sinh , cosh , tanh , asinh , acosh , atanh |
Hyperbolic functions |
imul , clz32 |
32 bit integer multiplication, count leading zeroes |
n
indicates big integer: 18446744073709551616n
let n = 18446744073709551616n n * n // 340282366920938463463374607431768211456n
BigInt(integer expression)
BigInt
with other numbers:
let offset = 1 n + offset // Error n + BigInt(offset) // Ok
BigInt.asIntN(bits, n)
and BigInt.asUintN(bits, n)
reduce modulo 2bits
into [-2bits
- 1...2bits
- 1 - 1] or [0...2bits
- 1]Date
measured in “milliseconds” (adjusted for leap seconds) since the “epoch” (midnight of January 1, 1970 UTC).new Date()
yields current date/time.new Date(milliseconds)
constructs Date
from milliseconds.
new Date(10**12) // 2001-09-09T01:46:40.000Z
new Date(year, zeroBasedMonth, day, hours, minutes, seconds, milliseconds)
Day
is optional.const brendanEichsBirthday = new Date(1961, 6 /* July */, 4)
zeroBasedMonth
, day
, hours
, etc. silently roll.new Date(2019, 13, -2) // 2020-01-28
YYYY-MM-DDTHH:mm:ss.sssZ (with literal T
, Z
):
const firstMillennialUTCnoon = new Date('2000-01-01T12:00:00.000Z')
±YYYYYY
for years outside 0000
...9999
Date(...)
without new
constructs string (in non-standard format).Date.UTC(year, zeroBasedMonth, day, hours, minutes, seconds, milliseconds)
year
optional.Date.parse(dateString)
YYYY-MM-DDTHH:mm:ss.sssZ
format.Date.now()
is current date/time.Date
object.getHours
/setHours
, not get
/set
methods.getFullYear
, getMonth
(0-11), getDate
(1-31), getHours
(0-23), getMinutes
, getSeconds
, getMilliseconds
setFullYear
, setMonth
, and so on.
birthday.setMonth(13)
getYear
, setYear
obsolete.getUTCFullYear
, getUTCMonth
, and so on.setUTCFullYear
, setUTCMonth
, and so on.toIsoString
yields string in YYYY-MM-DDTHH:mm:ss.sssZ
format.toString
, toDateString
, toTimeString
yield “humanly readable” string in local time zone, or only the date/time portion:
'Tue Jul 09 2019 07:23:48 GMT+0200 (GMT+02:00)' 'Tue Jul 09 2019' '07:23:48 GMT+0200 (GMT+02:00)'
toUTCString
yields “humanly readable” string in UTC:
'Tue, 09 Jul 2019 05:23:48 GMT'
toLocaleString
, toLocaleDateString
, toLocaleTimeString
yield localized string:
d.toLocaleTimeString('en-US') // '7:23:48 AM' d.toLocaleTimeString('de') // '07:23:48'