Number Literals

• Decimal numbers: 42, 0.42
• Exponential notation: 4.2e-10
• Hexadecimal: 0x2A
• Binary: 0b101010
• Octal: 0o52
• Old-style octal syntax invalid in strict mode: 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 and Methods

• Static Number functions isFinite, isNaN
• Do not use the global functions isFinite, isNaN
• These functions convert other types to numbers first: isNaN('Fred'), isFinite([0]) are true
• Global functions parseFloat, parseInt, also in Number.
• Static Number functions isInteger, isSafeInteger
• Number instance methods for floating-point formatting toFixed, toExponential, toPrecision:
• Math.sqrt(2).toPrecision(6) is '1.41421'

Constants

• Number constants MIN_VALUE, MAX_VALUE, MIN_SAFE_INTEGER, MAX_SAFE_INTEGER, EPSILON
• Also NaN, POSITIVE_INFINITY, NEGATIVE_INFINITY
• Same as global NaN, Infinity, -Infinity
• Math constants E, PI, SQRT2, SQRT1_2,LN2, LN10, LOG2E, LOG10E

Static Math Functions

 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, -3Also 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

Big Integers

• Arbitrary precision integers.
• Suffix n indicates big integer: 18446744073709551616n
let n = 18446744073709551616n
n * n // 340282366920938463463374607431768211456n
• Or create as BigInt(integer expression)
• Cannot mix BigInt with other numbers:
let offset = 1
n + offset // Error
n + BigInt(offset) // Ok
• Just two static functions: BigInt.asIntN(bits, n) and BigInt.asUintN(bits, n) reduce modulo 2bits into [-2bits - 1...2bits - 1 - 1] or [0...2bits - 1]

Constructing Dates

• Date measured in “milliseconds” (adjusted for leap seconds) since the “epoch” (midnight of January 1, 1970 UTC).
• Valid range ±100,000,000 days from epoch.
• new Date() yields current date/time.
• new Date(milliseconds) constructs Date from milliseconds.
• new Date(10**12) // 2001-09-09T01:46:40.000Z
• Caution: Dates are converted to numbers in arithmetic expressions.

Constructing Calendar Dates

• Construct local calendar date/time: new Date(year, zeroBasedMonth, day, hours, minutes, seconds, milliseconds)
• Everything starting from Day is optional.
• const brendanEichsBirthday = new Date(1961, 6 /* July */, 4)
• Caution: Out-of-range zeroBasedMonth, day, hours, etc. silently roll.
• new Date(2019, 13, -2) // 2020-01-28
• For UTC, construct with date string in format YYYY-MM-DDTHH:mm:ss.sssZ (with literal T, Z):
• const firstMillennialUTCnoon = new Date('2000-01-01T12:00:00.000Z')
• Use ±YYYYYY for years outside 0000...9999
• Caution: Other string formats may be accepted, but their format is not standardized.
• Caution: Date(...) without new constructs string (in non-standard format).

Static Date Functions

• Date.UTC(year, zeroBasedMonth, day, hours, minutes, seconds, milliseconds)
• Everything after year optional.
• Date.parse(dateString)
• Guaranteed support for YYYY-MM-DDTHH:mm:ss.sssZ format.
• Other formats implementation-dependent.
• Date.now() is current date/time.
• All of these yield milliseconds, not Date object.

Getters and Setters

• Traditional getter/setter methods getHours/setHours, not get/set methods.
• Local time: getFullYear, getMonth (0-11), getDate (1-31), getHours (0-23), getMinutes, getSeconds, getMilliseconds
• Setters setFullYear, setMonth, and so on.
• Setters silently roll: birthday.setMonth(13)
• getYear, setYear obsolete.
• UTC variants getUTCFullYear, getUTCMonth, and so on.
• Setters setUTCFullYear, setUTCMonth, and so on.

Date Formatting

• 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'