Transcript from the "NaN" Lesson
[00:00:31] You can't take a string and divide it by two, that doesn't make any sense. That's not a mathematic operation. I can't make Hello, class into a number and then divide it by two, so what am I gonna get? Well, I'm actually get a special kind of value called the NaN value.
[00:00:46] So something here is gonna have the NaN value. And it's gonna have it specifically because the divide operator on line three tried to turn the value Hello, class, that string, into a number and that's not a valid number. [LAUGH] So we end up this invalid number called NaN.
[00:01:04] Now a lot of people think NaN means is not a number, but it's not quite that. And I wanna call this out because we have a utility called Number.isNaN which would tell us, yes, you do in fact have a NaN. But let me ask you a question, is greeting a number?
[00:01:21] Well, clearly greeting is not a number, clearly it's a string. So you'd say, no, it's not a number. And it's definitely not a number, but is it the not a number special NaN value? And the answer is no, it's not. If we say, Number.isNaN for greeting, we get false.
[00:01:39] So NaN is a special value that sort of indicates that we've had an invalid numeric operation of some sort. And you should be aware that you should be testing for those. Any place that you do math in your programs, you need to be aware that NaNs can happen.
[00:01:54] And once you get a NaN, it's gonna mess up all the other math, so it sort of pollutes all the other operations. It's a really good idea to get familiar with testing the results of mathematic operations, to make sure that you didn't get a NaN.