NaN and Infinity

The full text of this chapter, to read or revisit. To practise, do the exercises.

β–Ά Do the exercises

Exercise 1 β€” What is NaN?

Sometimes in JavaScript, you'll try to do math that doesn't make sense. Instead of crashing your program, JavaScript gives you a special value: NaN.

NaN stands for Not a Number. It's what you get when a math operation fails or produces an undefined result.

// Common ways to get NaN let result1 = 0 / 0; let result2 = Math.sqrt(-1); console.log(result1, result2);

NaN is still a "number"?!

Yes, it's confusing! Even though NaN means "Not a Number", JavaScript considers it to be of type number. This is one of JavaScript's quirks you just have to remember.

Exercise 2 β€” What is Infinity?

JavaScript also has a special value for numbers that are too large to represent, or when you divide by zero: Infinity.

There are two infinities: positive (Infinity) and negative (-Infinity). They represent values beyond the limits of what JavaScript can handle.

// Common ways to get Infinity let result1 = Infinity; let result2 = -Infinity; let result3 = 1 / 0; console.log(result1, result2, result3);

Infinity is a number too!

Unlike in math class, dividing by zero doesn't cause an error in JavaScript. It gives you Infinity (or -Infinity if the numerator is negative). And yes, typeof Infinity is also "number"!

Exercise 3 β€” Which operations produce NaN?

Look at these operations. Which one will produce NaN?

Options
β€’ Math.sqrt(-25)
β€’ 10 / 0
β€’ 5 * 2
β€’ 100 - 50

Hint : NaN appears when a math operation doesn't have a valid result. Can you take the square root of a negative number in real math?

Exercise 4 β€” Understanding division by zero

What is the result of this code?

let codemonPower = 42; let divisor = 0; let result = codemonPower / divisor; console.log(result);
Options
β€’ Infinity
β€’ NaN
β€’ Error: Division by zero
β€’ 0

Hint : In JavaScript, dividing a positive number by zero doesn't cause an error. What special value represents something beyond limits?

Exercise 5 β€” Checking for NaN

To check if a value is NaN, use the Number.isNaN() function:

let result1 = 0 / 0; let result2 = 10 / 2; console.log(Number.isNaN(result1)); console.log(Number.isNaN(result2));

Exercise 6 β€” Checking for Infinity

To check if a number is finite (not Infinity and not NaN), use Number.isFinite():

let normal = 42; let infinite = 1 / 0; let notANumber = 0 / 0; console.log(Number.isFinite(normal)); console.log(Number.isFinite(infinite)); console.log(Number.isFinite(notANumber));

Exercise 7 β€” Checking for NaN correctly

Complete the code to check if the calculation result is NaN.

Hint : Use the strict version that starts with Number and uses dot notation.

Exercise 8 β€” Match operations with their results

Match each JavaScript expression with what it produces.

Items to match
β€’ 0 / 0
β€’ 5 / 0
β€’ -10 / 0
β€’ Math.sqrt(-4)
β€’ Infinity - Infinity
Definitions
β€’ NaN
β€’ Infinity
β€’ -Infinity
β€’ NaN
β€’ NaN

Exercise 9 β€” Understanding NaN

What will this code output?

let result1 = 0 / 0; let result2 = Math.sqrt(-1); console.log(result1); console.log(result2);
Options
β€’ NaN and NaN
β€’ 0 and -1
β€’ undefined and NaN
β€’ Infinity and NaN

Hint : Both operations produce invalid mathematical results. What value represents 'Not a Number'?

Back to the course