Skip to content

Globals and Numbers and Maths

JavaScript keeps its tools for working with numbers in three places. A few are global functions that can be called from anywhere, some are properties and methods of Number, and the rest belong to the Math object. Knowing which is which saves a search every time a calculation needs rounding, parsing or a maximum.

parseInt(), parseFloat(), isNaN() and isFinite() are global, so they need no object in front of them. ES2015 added copies of them to Number, and Number.parseInt and Number.parseFloat are the very same functions as the global ones:

console.log(parseInt === Number.parseInt);
console.log(parseFloat === Number.parseFloat);
console.log(isNaN('hello'));
console.log(Number.isNaN('hello'));

This prints:

true
true
true
false

isNaN and Number.isNaN are not the same, though. The global isNaN converts its argument to a number first, so the text 'hello' becomes NaN and the answer is true. Number.isNaN converts nothing and is true only for the value NaN itself, which makes it the one to use. The same difference holds between isFinite and Number.isFinite. NaN and isNaN has more on this.

Methods such as toFixed() are called on a number value, while checks such as Number.isInteger() and limits such as Number.MAX_SAFE_INTEGER are called on Number itself:

const price = 18.756;
console.log(Number.parseInt('18.756'));
console.log(price.toFixed(2));
console.log(typeof price.toFixed(2));
console.log(Number.isInteger(price));
console.log(Number.MAX_SAFE_INTEGER);

This prints:

18
18.76
string
false
9007199254740991

toFixed() returns a string, not a number, because its job is formatting for display. Rounding and Formatting Numbers covers it with its siblings, and ParseInt and ParseFloat covers reading numbers out of text.

Math is a plain object that holds constants and functions. It is not a constructor, so there is no new Math(), and every member is called on Math directly:

const temperatures = [14.2, 17.8, 11.5, 19.1, 16.4];
console.log(Math.max(...temperatures));
console.log(Math.min(...temperatures));
console.log(Math.round(17.8));
console.log(Math.floor(17.8));
console.log(Math.ceil(11.5));
console.log(Math.abs(-4));
console.log(Math.sqrt(81));
console.log(2 ** 10);
console.log(Math.PI);

This prints:

19.1
11.5
18
17
12
4
9
1024
3.141592653589793

Math.max and Math.min take separate arguments rather than an array, so the spread syntax ... unpacks the array into them. floor rounds down and ceil rounds up. The exponent operator ** does the job of Math.pow and is the usual choice in current code. Math.random() has a page of its own in Randomising in JavaScript.

Math.max has two results that surprise people. With no arguments it returns -Infinity, because that is the value every other number beats, and if any argument cannot be converted to a number it returns NaN:

console.log(Math.max());
console.log(Math.max(3, 'seven'));

This prints:

-Infinity
NaN

Spreading an empty array into Math.max gives the first case, so check the length of the array before relying on the answer.

Steve Griffith covers this topic in his video Globals and Numbers and Math. Oh My..