Null vs. Undefined
<p>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 <a href="/courses/video/5/157/Using-Variables.html">variable</a> along with a <a href="/courses/video/5/161/Numbers-int-And-unit.html">data type</a> but don't assign any value to it, then the variable is empty. What does <b>empty</b> 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.</p> <h2>Undefined</h2> <p>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 <b>undefined</b>. 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 <a href="/courses/video/5/161/Numbers-int-And-unit.html">data type</a> is considered as an <b>Undefined Variable</b>.</p> <h2>Comparing null and Undefined</h2> <p>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 <a href="/courses/video/7/199/Explicit-Equality.html">explicit equality</a>, 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 <a href="/courses/video/7/199/Explicit-Equality.html">explicitly</a> equal.</p> <h2>Comparing null and Undefined variables</h2> <p>Let's declare a <a href="/courses/video/5/157/Using-Variables.html">variable</a> <b>var str:String</b>; No value is assigned to str, so it is null. If we write <b>trace(str==null)</b>, the result would be true. Similarly, if we declare a variable <b>var num</b>; there is no data type associated with it in spite of being empty. Now if we write <b>trace(num==Undefined)</b>, the value would be true. Let's just interchange the variables for these two data types. For example, if we write <b>trace(str==Undefined)</b> or <b>trace(num==null)</b>, the result would still be true as both refer to nothing. But if we try to compare the explicit equality and write <b>trace(num===null)</b>, the result would be false because though both refer to Nothing, they are of different 'nothing' types which means they aren't explicitly equal.</p> <p>Let's now compare only the <a href="/courses/video/5/157/Using-Variables.html">variables</a> without the involvement of the keywords. If we write <b>trace(str==num)</b>or <b>trace(num==str)</b>, the result would be true without any warning. When we compare <b>trace(str===num)</b> or <b>trace(num===str)</b>, the result would now be false without any warning showing that they are <a href="/courses/video/7/199/Explicit-Equality.html">explicitly</a> unequal. This shows that there is no warning generated if we compare only the <a href="/courses/video/5/157/Using-Variables.html">variables</a> 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, <b>var str: *</b>; The application then comes to know that the variable in not defined intentionally.</p>