Home » Courses » Smooth Operator » Null vs. Undefined

### Smooth Operator

1. Overview (01:49)
2. Default Values (06:41)
3. Equality (06:40)
4. Rational Operators (05:52)
5. From Rational to Boolean (05:26)
6. Equality Operators (05:59)
7. Comparison Between Different Types (04:15)
8. Explicit Equality (08:05)
9. Logical Operator (09:52)
10. There Are Many Ways To Say No (04:15)
11. Null vs. Undefined (06:20)

### 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.

Got A Question?

### 'I love being Prime'

Have a question but don't want to ask it publicly? If you are a prime member just ask right here and we will get back to you within 48 hours. Not prime yet? no worries you can ask publicly below.

## Act now while we have available seating

Our on line school is private, affordable and interactive with trainer support.
Act now save your seat before someone else takes yours.