JavaScript Numbers and Dates - Exercise 1
Number.isFinite() returns true only for numbers that are neither Infinity nor NaN. It does not coerce strings, so a numeric string is rejected instead of silently converted.
Number.isFinite(42);
Use Number() when the entire string should represent one number; it returns NaN for invalid input. Use parseInt() or parseFloat() when reading a numeric prefix from a larger string.
const total = Number('19.5');
NaN means that a numeric operation did not produce a meaningful number. It is still a value of type number, and because it is not equal to itself, use Number.isNaN() to test for it.
const result = Number('not a number');
console.log(Number.isNaN(result));
Math.floor() rounds toward negative infinity, returning the greatest integer less than or equal to the input. This differs from truncation for negative decimals: Math.floor(-2.3) is -3, not -2.
Math.floor(4.9);
Math.random() returns a pseudorandom floating-point value from 0 inclusive to 1 exclusive. Multiply and floor it when you need a random integer in a chosen range.
const randomValue = Math.random();
Date.now() returns the current Unix timestamp in milliseconds since January 1, 1970 UTC. Store it when you need to compare elapsed time or create a corresponding Date object.
const timestamp = Date.now();
Pass an ISO 8601 string to the Date constructor to create a predictable instant. Including a time-zone marker, such as Z for UTC, prevents the same text from being interpreted differently across environments.
const launchDate = new Date('2026-09-23T12:00:00Z');
Call getFullYear() to read the year in the local time zone. If the result must be independent of the user's location, use getUTCFullYear() instead.
const year = launchDate.getFullYear();
A Date stores an instant, but local getters display that instant in the host time zone. UTC getters use Coordinated Universal Time, so the hour or calendar day can differ near midnight.
date.getHours();
date.getUTCHours();
Use toISOString() for a consistent, readable UTC string in ISO 8601 format. It is useful for APIs and storage because it does not depend on the viewer's local time zone.
const formatted = new Date().toISOString();