Javascript · Numbers
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
.
let result = 0 / 0; console.log(____________(result));
Options: Number . isNaN isFinite
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