Modern JavaScript: New Number Methods
Welcome to the New Number Methods lesson!
This lesson is shown as static text below. However, it's designed to be used interactively. Click the button below to start!
Number.isNaNisn't the only new property onNumber. For example,Number.isFinitechecks that a value is neitherInfinityor-Infinity. UsingisFinitehelps you to avoid a possible mistake: checking for infinite values by doingx === Infinity. That would miss-Infinity, which is a different value!>
Infinity === -Infinity;Result:
false
>
Number.isFinite(5);Result:
true
>
Number.isFinite(Infinity);Result:
false
>
Number.isFinite(-Infinity);Result:
false
>
Number.isFinite('a string');Result:
false
isFinitereturnsfalseforNaN. That makes some sense:NaNisn't a number, so it's definitely not a finite number!>
Number.isFinite(NaN);Result:
false
All numbers in JavaScript are floating point, which means that they become imprecise past a certain threshold. This can be especially dangerous when dealing with numbers that we think of as integers.
Modern versions of JavaScript give us
Number.MIN_SAFE_INTEGERandNumber.MAX_SAFE_INTEGERto help here. They define the smallest and largest numbers that can be safely treated as integers.>
Number.MAX_SAFE_INTEGER;Result:
>
Number.MIN_SAFE_INTEGER;Result:
>
Number.MIN_SAFE_INTEGER === -Number.MAX_SAFE_INTEGER;Result:
true
When a number is larger than
MAX_SAFE_INTEGERor smaller thanMIN_SAFE_INTEGER, floating point arithmetic becomes imprecise. For example, we can come up with a situation wherex + 1 === x + 2in JavaScript.>
Number.MAX_SAFE_INTEGER + 1 === Number.MAX_SAFE_INTEGER + 2;Result:
true
Integer numbers between
MIN_SAFE_INTEGERandMAX_SAFE_INTEGERbehave normally.>
Number.MAX_SAFE_INTEGER - 1 === Number.MAX_SAFE_INTEGER;Result:
false
There's also a
Number.isSafeIntegermethod that checks both the lower and upper bounds for us.>
Number.isSafeInteger(Number.MAX_SAFE_INTEGER);Result:
true
>
Number.isSafeInteger(Number.MAX_SAFE_INTEGER + 1);Result:
false
>
Number.isSafeInteger(Number.MIN_SAFE_INTEGER);Result:
true
>
Number.isSafeInteger(Number.MIN_SAFE_INTEGER - 1);Result:
false
>
Number.isSafeInteger('a string');Result:
false
Here's a code problem:
Write a
safeIntegerMultiplyfunction that multiplies two numbers. If the product of the numbers isn't a safe integer, it should throw an error. (You can throw withthrow new Error('Product is an unsafe integer').)A hint: you should check whether the product of the two numbers (
x * y) is a safe integer or not. It's possible for the product to be unsafe even if thexandyare both safe on their own. For example, anxof 2 and ayof 4503599627370496 are both safe on their own, but their product of 9007199254740992 is not safe.function safeIntegerMultiply(x, y) {const product = x * y;if (!Number.isSafeInteger(product)) {throw new Error('Product is an unsafe integer');}return product;}const product = safeIntegerMultiply(3, 5);let [threwForLargeNumber, threwForSmallNumber] = [false, false];try { safeIntegerMultiply(2, 4503599627370496); }catch { threwForLargeNumber = true; }try { safeIntegerMultiply(2, -4503599627370496); }catch { threwForSmallNumber = true; }[product, threwForLargeNumber, threwForSmallNumber];- Goal:
[15, true, true]
- Yours:
[15, true, true]
One final math-related feature. In the past, we used
Math.powto compute exponents. Now JavaScript has an exponentiation operator,**. This is the same syntax used in Ruby, Python, and other languages.>
Math.pow(2, 3);Result:
>
2 ** 3;Result:
8