Notes Javascript - Numbers & Dates



Numbers & Dates

Javascript allows for many different number types and bases.

Number Type Indicator/Prefix/Suffix Notes Examples
Decimal None Includes integers and floating point numbers. 1234567890 42 12.9
Binary Prefixed with a zero followed by the letter B
(upper or lower case B)
Binary can only be zero or one. 0b00010011 0B01010110
Octal Prefixed with a zero followed by the letter O
(Upper or lower case O)
Octal can only be between zero and eight. 0o755 // 493 0O644 // 420
Hexidecimal Prefixed with a zero followed by the letter X
(Upper or lower case X)
Hexidecimal numbers can contain digits between zero and nine as well as the letters, A through F. 0xFFFF 0XABC4
Exponent A number followed by the letter E (upper or lower case), followed by another number. If a number literal has an exponent part, then the value of the literal is computed by multiplying the part before the e by (10 raised to the power of the part after the e).
So 100 and 1e2 are the same number.
1e2 can be written:
(1 x (10^2)) = 100.
Another example 2e2 can be written:
(2 x (10^2)) = 200.
0e-5 // (0 x (10^-5)) = 0 0e+5 // (0 x (10^5)) = 0 5e1 // (5 x (10^1)) = 50 175e-2 // (175 x (10^-2)) = 1.75 1e3 // (1 x (10^3)) = 1000 1e-3 // (1 x (10^-3)) = 0.001 2E2 // (2 x (10^2)) = 200
BigInt A number suffixed with the lowercase letter N The highest integer with regular notation is:
(2^53 - 1)
1234n -1234567890987654321n

x = 1234567890; // DECIMAL x = 12; // DECIMAL x = 0888; // 888 PARSED AS DECIMAL (See Note Below) x = 0777; // PARSED AS 777 OCTAL (See Note Below) x = 0b00010101; // BINARY 21 x = 0b1111; // BINARY 15 x = 0o755; // OCTAL 493 x = 0o377; // OCTAL 255 x = 0xFF; // HEXIDECIMAL 15 x = 0x5F; // HEXIDECIMAL 95 x = 4e3; // EXPONENTIAL (4 x (10^3)) = 4000 x = 72e2; // EXPONENTIAL (72 x (10^2)) = 7200 255; // TWO HUNDRED AND FIFTY FIVE 255.0; // SAME NUMBER 255 === 255.0; // TRUE 255 === 0xff; // TRUE (HEXADECIMAL NOTATION) 255 === 0b11111111; // TRUE (BINARY NOTATION) 255 === 0.255e3; // TRUE (DECIMAL EXPONENTIAL NOTATION)

Decimal literals can start with a zero (0) followed by another decimal digit, but if all digits after the leading 0 are smaller than 8, the number is interpreted as an octal number.
This is considered a legacy syntax, and number literals prefixed with 0, whether interpreted as octal or decimal, cause a syntax error in strict mode, so, use the 0o prefix instead.


Number Object

Although you can create a number using the Number object constructor, it is most often used to test or set the value of something else.

const biggestNum = Number.MAX_VALUE; const smallestNum = Number.MIN_VALUE; const infiniteNum = Number.POSITIVE_INFINITY; const negInfiniteNum = Number.NEGATIVE_INFINITY; const notANum = Number.NaN;

Number Object Properties
Property Description
Number.MAX_VALUE The largest positive representable number (1.7976931348623157e+308)
Number.MIN_VALUE The smallest positive representable number (5e-324)
Number.NaN Special "not a number" value
Number.NEGATIVE_INFINITY Special negative infinite value; returned on overflow
Number.POSITIVE_INFINITY Special positive infinite value; returned on overflow
Number.EPSILON Difference between 1 and the smallest value greater than 1 that can be represented as a Number (2.220446049250313e-16)
Number.MIN_SAFE_INTEGER Minimum safe integer in JavaScript (-2^53 + 1, or -9007199254740991)
Number.MAX_SAFE_INTEGER Maximum safe integer in JavaScript (+2^53 - 1, or +9007199254740991)
Number Object Methods
Method Description
Number.parseFloat() Parses a string argument and returns a floating point number. Same as the global parseFloat() function.
Number.parseInt() Parses a string argument and returns an integer of the specified radix or base. Same as the global parseInt() function.
Number.isFinite() Determines whether the passed value is a finite number.
Number.isInteger() Determines whether the passed value is an integer.
Number.isNaN() Determines whether the passed value is NaN. More robust version of the original global isNaN().
Number.isSafeInteger() Determines whether the provided value is a number that is a safe integer.

The Number prototype provides methods for retrieving information from Number objects in various formats. The following table summarizes the methods of Number.prototype.

Prototype Methods
Method Description
toExponential() Returns a string representing the number in exponential notation.
toFixed() Returns a string representing the number in fixed-point notation.
toPrecision() Returns a string representing the number to a specified precision in fixed-point notation.

Math Object

The built-in Math object has properties and methods for mathematical constants and functions. For example, the Math object's PI property has the value of pi (3.141...), which you would use in an application as


Similarly, standard mathematical functions are methods of Math.
These include trigonometric, logarithmic, exponential, and other functions.
For example, if you want to use the trigonometric function sine, you would write:


Note that all trigonometric methods of Math take arguments in radians.

Math Object Methods
Method Description
abs() Absolute value
Standard trigonometric functions; with the argument in radians.
Inverse trigonometric functions; return values in radians.
Hyperbolic functions; argument in hyperbolic angle.
Inverse hyperbolic functions; return values in hyperbolic angle.
Exponential and logarithmic functions.
Returns the largest/smallest integer less/greater than or equal to an argument.
Returns the minimum or maximum (respectively) value of a comma separated list of numbers as arguments.
random() Returns a random number between 0 and 1.
Rounding and truncation functions.
Square root, cube root, Square root of the sum of square arguments.
sign() The sign of a number, indicating whether the number is positive, negative or zero.
Number of leading zero bits in the 32-bit binary representation.
The result of the C-like 32-bit multiplication of the two arguments.



Date Object

JavaScript Date objects represent a single moment in time in a platform-independent format. Date objects encapsulate an integral number that represents milliseconds since the midnight at the beginning of January 1, 1970, UTC (the epoch).

There are two groups of Date methods: one group gets and sets various date components by interpreting the timestamp as a local time, while the other uses UTC.

Date Object Methods
Component Get Local Set Local Get UTC Set UTC
Year getFullYear() setFullYear() getUTCFullYear() setUTCFullYear()
Month getMonth() setMonth() getUTCMonth() setUTCMonth()
Date (of month) getDate() setDate() getUTCDate() setUTCDate()
Hours getHours() setHours() getUTCHours() setUTCHours()
Minutes getMinutes() setMinutes() getUTCMinutes() setUTCMinutes()
Seconds getSeconds() setSeconds() getUTCSeconds() setUTCSeconds()
Milliseconds getMilliseconds() setMilliseconds() getUTCMilliseconds() setUTCMilliseconds()
Day (of week) getDay() N/A getUTCDay() N/A


Date Time String Format

There are many ways to format a date as a string. The JavaScript specification only specifies one format to be universally supported: the date time string format, a simplification of the ISO 8601 calendar date extended format. The format is as follows:


Date Format Strings
String Description
YYYY The year, with four digits (0000 to 9999), or as an expanded year of + or - followed by six digits. The sign is required for expanded years. -000000 is explicitly disallowed as a valid year.
MM The month, with two digits (01 to 12). Defaults to 01.
DD The day of the month, with two digits (01 to 31). Defaults to 01.
T A literal character, which indicates the beginning of the time part of the string. The T is required when specifying the time part.
HH The hour, with two digits (00 to 23). As a special case, 24:00:00 is allowed, and is interpreted as midnight at the beginning of the next day. Defaults to 00.
mm The minute, with two digits (00 to 59). Defaults to 00.
ss The second, with two digits (00 to 59). Defaults to 00.
sss The millisecond, with three digits (000 to 999). Defaults to 000.
Z The timezone offset, which can either be the literal character Z (indicating UTC), or + or - followed by HH:mm, the offset in hours and minutes from UTC.
Static Methods
Method Description Returns the numeric value corresponding to the current timeā€”the number of milliseconds elapsed since January 1, 1970 00:00:00 UTC, with leap seconds ignored.
Date.parse() Parses a string representation of a date and returns the number of milliseconds since 1 January, 1970, 00:00:00 UTC, with leap seconds ignored.
Date.UTC() Accepts the same parameters as the longest form of the constructor (i.e. 2 to 7) and returns the number of milliseconds since January 1, 1970, 00:00:00 UTC, with leap seconds ignored.

Creating a date from a string has a lot of behavior inconsistencies. See date time string format for caveats on using different formats.

const today = new Date(); const birthday = new Date("December 17, 1995 03:24:00"); // DISCOURAGED: may not work in all runtimes const birthday2 = new Date("1995-12-17T03:24:00"); // This is standardized and will work reliably const birthday3 = new Date(1995, 11, 17); // the month is 0-indexed const birthday4 = new Date(1995, 11, 17, 3, 24, 0); const birthday5 = new Date(628021800000); // passing epoch timestamp

Formats of toString method return values

const date = new Date("2020-05-12T23:50:21.817Z"); date.toString(); // Tue May 12 2020 18:50:21 GMT-0500 (Central Daylight Time) date.toDateString(); // Tue May 12 2020 date.toTimeString(); // 18:50:21 GMT-0500 (Central Daylight Time) date[Symbol.toPrimitive]("string"); // Tue May 12 2020 18:50:21 GMT-0500 (Central Daylight Time) date.toISOString(); // 2020-05-12T23:50:21.817Z date.toJSON(); // 2020-05-12T23:50:21.817Z date.toUTCString(); // Tue, 12 May 2020 23:50:21 GMT date.toLocaleString(); // 5/12/2020, 6:50:21 PM date.toLocaleDateString(); // 5/12/2020 date.toLocaleTimeString(); // 6:50:21 PM