NaN and Infinity
The full text of this chapter, to read or revisit. To practise, do the exercises.
βΆ Do the exercisesExercise 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?
Math.sqrt(-25)10 / 05 * 2100 - 50Hint : 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);InfinityNaNError: Division by zero0Hint : 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.
0 / 05 / 0-10 / 0Math.sqrt(-4)Infinity - InfinityNaNInfinity-InfinityNaNNaNExercise 9 β Understanding NaN
What will this code output?
let result1 = 0 / 0;
let result2 = Math.sqrt(-1);
console.log(result1);
console.log(result2);NaN and NaN0 and -1undefined and NaNInfinity and NaNHint : Both operations produce invalid mathematical results. What value represents 'Not a Number'?