Execute Program

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.isNaN isn't the only new property on Number. For example, Number.isFinite checks that a value is neither Infinity or -Infinity. Using isFinite helps you to avoid a possible mistake: checking for infinite values by doing x === Infinity. That would miss -Infinity, which is a different value!

  • >
    Infinity === -Infinity;
    Result:
    falsePass Icon
  • >
    Number.isFinite(5);
    Result:
    truePass Icon
  • >
    Number.isFinite(Infinity);
    Result:
    falsePass Icon
  • >
    Number.isFinite(-Infinity);
    Result:
    falsePass Icon
  • >
    Number.isFinite('a string');
    Result:
    falsePass Icon
  • isFinite returns false for NaN. That makes some sense: NaN isn't a number, so it's definitely not a finite number!

  • >
    Number.isFinite(NaN);
    Result:
    falsePass Icon
  • 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_INTEGER and Number.MAX_SAFE_INTEGER to 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:
    truePass Icon
  • When a number is larger than MAX_SAFE_INTEGER or smaller than MIN_SAFE_INTEGER, floating point arithmetic becomes imprecise. For example, we can come up with a situation where x + 1 === x + 2 in JavaScript.

  • >
    Number.MAX_SAFE_INTEGER + 1 === Number.MAX_SAFE_INTEGER + 2;
    Result:
    truePass Icon
  • Integer numbers between MIN_SAFE_INTEGER and MAX_SAFE_INTEGER behave normally.

  • >
    Number.MAX_SAFE_INTEGER - 1 === Number.MAX_SAFE_INTEGER;
    Result:
    falsePass Icon
  • There's also a Number.isSafeInteger method that checks both the lower and upper bounds for us.

  • >
    Number.isSafeInteger(
    Number.MAX_SAFE_INTEGER
    );
    Result:
    truePass Icon
  • >
    Number.isSafeInteger(
    Number.MAX_SAFE_INTEGER + 1
    );
    Result:
    falsePass Icon
  • >
    Number.isSafeInteger(
    Number.MIN_SAFE_INTEGER
    );
    Result:
    truePass Icon
  • >
    Number.isSafeInteger(
    Number.MIN_SAFE_INTEGER - 1
    );
    Result:
    falsePass Icon
  • >
    Number.isSafeInteger(
    'a string'
    );
    Result:
    falsePass Icon
  • Here's a code problem:

    Write a safeIntegerMultiply function that multiplies two numbers. If the product of the numbers isn't a safe integer, it should throw an error. (You can throw with throw 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 the x and y are both safe on their own. For example, an x of 2 and a y of 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]Pass Icon
  • One final math-related feature. In the past, we used Math.pow to 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:
    8Pass Icon