Lesson Description

The "NaN: Not a Number" Lesson is part of the full, Getting Started with JavaScript, v3 course featured in this preview video. Here's what you'd learn in this lesson:

Kyle explains the concept of "not a number" (N-A-N) in JavaScript, which is returned when trying to convert a non-numeric string to a number using parseInt. He clarifies that NaN is a special reserved keyword in JavaScript, representing a value that cannot be converted to a number. Kyle also discusses the confusion around NaN, including how NaN is of type number, but comparisons with NaN always return false, necessitating the use of the isNAN function to check for NaN. Additionally, Kyle touches on implicit type coercion in JavaScript, highlighting the differences between using double equals (==) and triple equals (===) for equality checks, emphasizing how JavaScript automatically coerces types with double equals.

Preview

Transcript from the "NaN: Not a Number" Lesson

[00:00:00]
>> WebDevSimplified: Now on top of even more confusing topics, we have the reference or the concept of not a number in JavaScript because we have the ability to parse an integer with this parse int function. You pass it in a string. But what happens when we pass in a string that has no numbers? For example, I'm passing in the string hello. I don't know how to convert that to a number, and neither does JavaScript, so it returns something called NaN.

[00:00:21]
It specifically is capital N, A, N, and it stands for not a number, and this is just a special type of reserved keyword in JavaScript just like undefined or null that references specifically a number that you try to convert into a number, but there's no way to convert into a number. So if you have a number and you can't actually convert it into a number, it gives you a NaN. So let's actually use this code real quick and just copy it over to look at it.

[00:00:45]
And you can see here when I try to convert this result, it is printing out NaN, and you'll also notice the type of this result, type of NaN returns number. So even though NaN stands for not a number, it still has the type of number. I know that's quite confusing, but essentially it's trying to represent a number and it can't, which is why it uses this NaN type even though the type of that is specifically a number.

[00:01:09]
Now there's even more oddities when it comes to not a number just to make the confusion even more difficult is that whenever you try to check to see if something is not a number, it'll always return false. Not a number is never equal to anything at all inside of JavaScript. Even itself, if I print out not a number, equal equal, not a number, that returns false because for some reason in JavaScript they hard coded it that not a number is never equal to literally anything at all.

[00:01:34]
The only way to actually check to see if something is not a number is to use a function called isNaN which stands for is not a number. So we'll copy over this code so we can take a look at it a little bit more in depth. We can see here we're parsing an integer that's clearly not a number. So A is not a number and B is clearly the number 5. So I can run this is not a number function on A and it prints out true just like we expect because that is not a number.

[00:01:58]
While when I call is not a number on B, that's going to return false because this thing is specifically a valid number and it is not that not a number type. I know this is quite confusing because like I said, if I were to check to see if A is equal to not a number that's going to return to me false. Just know that if you want to check for not a number, you specifically have to use the special is not a number function.

[00:02:20]
This has been the source of many bugs that I've run into in many other developers, so don't feel bad if you mess this one up or forget about it. It's a very common source of problems.

Learn Straight from the Experts Who Shape the Modern Web

  • 250+
    In-depth Courses
  • Industry Leading Experts
  • 24
    Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now