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.

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!

Overview

What are operators and why do we need them to create programs. We'll discuss various types of operators and their uses later in this course

01:49

Default Values

To understand operators, we first need to understand how variables work -- mainly when it comes to default values and why they differ for various variable types.

06:41

Equality

In this video we will compare between equality and assignment and learn how to work with both. We also differentiate between the assignment operator (=) and the equality operator (==)

06:40

Rational Operators

In this video we will see how to check if a variable is: bigger, smaller, greater or equals, smaller or equals and so on..... using rational operators

05:52

From Rational to Boolean

All the operators we worked with so far returned in our trace panel a value of true or false. In this video we'll see that connection of our operators results to the Boolean data type as we explore

05:26

Equality Operators

So far we've seen how to compare and see if values differ or match but we did not have a way to ask if not. In this sample we'll look into the equality operator(make it a bit more complex)

05:59

Comparison Between Different Types

In the heart of programming is the capability to compare between things. In this video we talk about comparing between different variable data types.

04:15

Explicit Equality

Sometimes knowing if something is equal just isn't enough and we need to know if it is explicitly equal and that's where explicit equality operator comes in...

08:05

Logical Operator

Logical operators enable us to compare a few things at a time such as AND, OR and NOT. We will learn the syntax to represent these operators using few examples

09:52

There Are Many Ways To Say No

doing exactly the oposite of the logical logic seams crazy but actually its a very great way to get things done. Let us learn how to say no in terms of programming

04:15

Null vs. Undefined

Why are there two ways of saying nothing? I ponder that as well as I try to explain their roles. In the process, we will learn the syntax and its application in code

06:20