Skip to content

1.9 Undefined Data Type

What is undefined?

Task 1: Declare a variable called value.

var value;

Note(1): var value means that create a variable called value.

Task 2: Declare a variable called age. Do not assign any value to it. Check its value in the console.

var age;
console.log(age)

You will get: undefined.

What is undefined? It simply means that the variable has not been assigned to any value yet.

Type of undefined

Task 3: Declare a variable called username. Check the typeof of this variable.

var username;
console.log(typeof username);

You will get undefined.

What?! undefined type is undefined. Yes!undefined is a data type in JavaScript like string, number, boolean, and null.

Task 4: Declare two variables; the first is called name, and the other one is called profession.

var name, profession;

or

var name;
var profession;

undefined is false

Task 5: Declare a variable called username. Do not assign any value to it. Then test if username is true or false.

var username;
if(username){
  console.log(true);
}else{
  console.log(false);
}

You will get: false.

Why? undefined is false in JavaScript.

Variable Hoisting

Task 6: If the username is equal to "Ali", declare a variable called hasKey, and set hasKey value to true. Otherwise, declare a variable called password, and set the password value to "123". Then display the value of hasKey and password in the console.

var username = "Ali";
if(username === "Ali"){
  var hasKey = true;
}else{
  var password = 123;
}
console.log(hasKey);
console.log(password);

The result will be:

true
undefined

Why?! It is how JavaScript reads the code:

1- The JavaScript engine reads the code line by line.

2- It assigns undefined to all the declared variables with the word var.

3- In our previous example; the values assigned to the variables are:

  username  undefined
  hasKey    undefined
  password  undefined

4- Then the JavaScript engine reads the code again line by line, and execute it.

5- It also assigns the values, we set, to the variables:

  username  Ali
  hasKey    true
  password  undefined

Herein, it does not execute var password = 123; because the condition does NOT satisfy. Let's practice more!

Task 7: Declare a variable called mark. Display mark value in the console. Then assign the value of 90 to mark. Display the mark value in the console again.

var mark;
console.log(mark);
mark = 90;
console.log(mark);

you will get:

undefined
90

How does it work?

  1. The JavaScript engine reads the code line by line.

  2. It assigns undefined to the declared variable mark.

  3. It executes the code line by line from top to bottom.

  4. console.log(mark);; mark has the value of undefined now.

  5. It assigns the value of 90 to mark.

  6. console.log(mark);; mark has the value of 90 now.

Null VS undefined

Task 8: Declare a variable called firstname, and check firstname type. Then declare a variable called mark. Assign the value of null to mark. Check mark type in the console.

var firstname;
console.log(typeof firstname);
var mark = null;
console.log(typeof null);

you will get:

undefined
object

What is the difference between undefined and null?

Note(2): undefined means that the variable is declared but no value has been assigned to it yet.

Note(3): null means that the variable is declared and null has been assigned to it as a value.

Note(4): null indicates a lack/absence of a value.

More Practice

Task 9: Declare 4 variables; a, b, c, d. Display their values in the console.

var a, b, c, d;
console.log(a, b, c, d);

you will get: undefined undefined undefined undefined

Note(5): To display multiple values in the console, we separate them by commas. For example, console.log(a, b, c, d)

Task 10: Declare a variable called lastname, then check whether lastname is equal to 'sth'. If yes, set the variable isKnown to true. If no, set the variable isKnown to false. Hint: use ternary operator.

var lastname;
var isknown = lastname === "sth" ? true : false;
console.log(isknown);

You will get: false.

why? Because lastname is undefined, and undefined is NOT equal to "sth". The condition is false, so isknown is false.