02Geek HTML5 and JavaScript, TypeScript, React, Flash, ActionScript online School

Null vs. Undefined

We have learned the concept of nothing earlier, but did you realize there are two different types of nothing? Here we'll look at both. When we define a variable along with a data type but don't assign any value to it, then the variable is empty. What does empty actually mean? Such a variable has nothing in it and is referred to as null. A memory location is defined for this variable, depending upon its data type. This is definitely a defined variable but with no real content: in other words it's undefined.

Undefined

So if a null variable is empty or has nothing in it, then what about an undefined one? If we declare a variable and don't even mention the data type for it, then such a variable is called undefined. Even though it has nothing in it like null, this variable has no memory defined for it. The compiler defines memory for only those variables whose data type is assigned. We can say that a declared variable with no data type is considered as an Undefined Variable.

Comparing null and Undefined

There is a difference between these two, but both of them are basically nothing. If we try to equate these two terms, the result would be true along with a warning that the comparison is not substantial. However, if we try to compare these two for explicit equality, the result would now be false with two warnings. This is because these are two different types of nothing (null and undefined) and are therefore not explicitly equal.

Comparing null and Undefined variables

Let's declare a variable var str:String; No value is assigned to str, so it is null. If we write trace(str==null), the result would be true. Similarly, if we declare a variable var num; there is no data type associated with it in spite of being empty. Now if we write trace(num==Undefined), the value would be true. Let's just interchange the variables for these two data types. For example, if we write trace(str==Undefined) or trace(num==null), the result would still be true as both refer to nothing. But if we try to compare the explicit equality and write trace(num===null), the result would be false because though both refer to Nothing, they are of different 'nothing' types which means they aren't explicitly equal.

Let's now compare only the variables without the involvement of the keywords. If we write trace(str==num)or trace(num==str), the result would be true without any warning. When we compare trace(str===num) or trace(num===str), the result would now be false without any warning showing that they are explicitly unequal. This shows that there is no warning generated if we compare only the variables without using keywords. Further, if we don't want the compiler to generate any error or a warning for Undefined Variables, we have to declare the variable along with * symbol in the following way, var str: *; The application then comes to know that the variable in not defined intentionally.

Video Transcript

00:00So let's talk about
00:02Undefined values and null values. We've met null already before when we've created a new string
00:10So if we create a new string and not assign any value to it and trace its value out it should come back as null
00:18Now how do we how do we if we want to check if a string equals null?
00:22How would we check that what we could do is ask is the string equals no without putting that null into a?
00:30Not to quote it because it's not a string. It's a value of no value
00:34So if we try to check if string equals null in our trace
00:38Then we should get the value of true
00:41Now if we wanted to we could have made it an explicit equals, but let's see what would happen if we would put our null into
00:48quotations if we run the application one more time the value that would come back is false because it's not a
00:55string null is a value of nothing which is a
01:00Standalone reference
01:02Okay, so we don't want to put in a quote because it's not it's not in a quote
01:06Okay, then it's the meaning of know me of know of no content very good
01:11So now we see what null is now. We've talked about the
01:15Undefined and if you've worked in action script two you've met undefined quite a lot of times well in action script three
01:22It's much more rare to encounter
01:25Naturally undefined the way we could create an undefined value is by introducing another kind of like a wild card if you create a new
01:34Variable and let's just call it object object for now although. We don't really know what an object is
01:40For now if we want to tell it to our object that it could be of any type
01:45We're not assigning to at any specific value now just by running the application this way
01:50That would be legitimate only in different compiling settings if you're running on flex if you're running on flash in a more strict mode
01:57It would generate an error
01:59But if we trace out our object right now just as is even without setting anything special to it
02:05The value that we should get back from our object is undefined and not null
02:11Well, it's because flash didn't even store didn't even convert it and store it in memory because it doesn't even know what type it is
02:19Now the the more proper way of defining a variable without a specific type in action script three is by defining it as the stock
02:27Is the type of star or as wild card?
02:31Which would be the same thing only in this way it would not generate any errors. It's basically
02:37Kind of explicitly telling to flash that we know that we didn't define it a type and we deliberately didn't define a type to it
02:44So now we see so now we see how we could get a null value
02:49from a string
02:51And we could see how we could be get an undefined value
02:56From an object or a variable that wasn't defined which we didn't define what type of variable it is
03:03now the big question in hand is does null and our null and
03:08Undefined equal to each other or in other words is null equals to
03:14Undefined
03:17Undefined
03:20Let's find out is null equal undefined
03:24Ooh
03:25First of all, we've got a compiling warning, but it's telling us the truth null is equal to undefined
03:31But if we look into our compiling warning warning, it's going to tell you
03:35Variable of the type of null cannot be undefined but from our scenarios
03:39It's it's just true nothing and nothing equals nothing. Although it's different types of nothing
03:45Now although this would generate true with an error a warning more accurately
03:55let's see what would happen if we try to trace our null and
03:59Explicitly say it equals to undefined
04:02What value do you think would come out in our output if we define if we output it this way?
04:12now we're getting two warnings and the value that is coming back is false because null is not
04:18Explicitly the same as undefined although they represent similar things
04:22null represents a variable that was
04:25Defined but was empty
04:28while undefined represents
04:31void of content completely
04:34so this would come back is false if we explicitly match them and
04:38It would come with a warning as well. See got a little typo up here
04:46But let's see what the warning what was the warning that we got here. So we got the same warning
04:51Okay, but we're going to ignore that warning for now because it's just for that for our experiments
04:56We could let's comment out our null and undefined and the last thing I want to talk about this sample is let's try to compare between
05:02Our string or str and compare it with our object
05:07Although we're expecting to get the same exact
05:10Answer right because not one of them is containing null and the others containing undefined
05:15So let's just see if that's really the case and that is the case only now
05:21We're not getting any compiling errors
05:24okay, so flash doesn't notice that differentiation between nothing and
05:31Nothing whenever you're comparing it inside of variables directly. Let's try a little bit more things
05:39Let's ask if object equals equals and we know object by default would because we didn't define it a type would equal undefined
05:47So let's check if it equals null
05:50and it should come back as true, but if we wanted to explicitly ask if it equals to null then we would ask explicitly and
06:01Then the value would come back as false
06:03And let's try that it also. Let's just copy that and let's try that also with our string
06:11Which we know by default does equal null so it would come back as true
06:15So this time around we're expecting a false and then the true

Ready to Level Up Your Skills?

Join thousands of learners on 02GEEK and start your journey to becoming a coding expert today!

Enroll Now for Free!