JavaScript Interview Day # 2 - Can You Explain Undefined And Null?

Have you ever been asked, “Can you explain null and undefined in JavaScript”? This question is very trivial but important. As a JavaScript developer, you must have absolute understanding about undefined and null.

Let us start by looking at the code below.

  1. var foo;  
  2. console.log(foo);   

Output of the above code listing would be “undefined” as shown in the image below.

JavaScript

What is going on here? Whenever you create a variable in JavaScript and do not assign any value to it, that variable will contain a value “undefined”. So basically, undefined represents the missing value in JavaScript.

Let us consider another code snippet.

  1. var foo = null;  
  2. console.log(foo);   

Output of above code listing would be “null”, as shown in the image below.

JavaScript

So, what is going on here? When you create a variable but are not sure what should be the value, you may assign it with the value null. Therefore, null is a primitive value in JavaScript.

What is null?

In JavaScript, null represents intentional absence of a value. It is a primitive JavaScript value. You can say, null indicates an object with no value.

JavaScript

You can assign null to a variable as indication of no value. It can be assigned to a variable. In JavaScript, null can be assigned to any variable as representation of no value. Therefore, JavaScript very much allows you to perform the task shown in the listing below.

  1. var foo = null;  
  2. console.log(foo);   

Every value has a type in JavaScript and type of null is object. Try running the below code.

  1. console.log(typeof (null));   

You will get the below output which shows that the type of null is object.

JavaScript

JavaScript never sets value null to any variable automatically. If you want to set variable value null, it has to be done programmatically.

JavaScript

In JavaScript arithmetic operations, null becomes zero. So if you try to perform the following operations -

  1. var foo = null;  
  2. var a = foo + 9;  
  3. var b = foo - 9;  
  4. var c = foo * 9;  
  5. var d = foo / 9;  
  6. console.log(a);  
  7. console.log(b);  
  8. console.log(c);  
  9. console.log(d);   

The output of above operation would be -

JavaScript

As you might have noticed that JavaScript is converting null value to zero value for all the arithmetic operations.

JavaScript

JavaScript treats null as zero for comparisons. However, keep in mind that null is not equal to zero. Try performing the below operations.

  1. var foo = null;  
  2.   
  3. if (foo > 9) {  
  4.     console.log('greater than 9');  
  5. }  
  6. if (foo < 9) {  
  7.     console.log('less than 9');  
  8. }  
  9.   
  10. if (foo > -9) {  
  11.     console.log('greater than -9');  
  12. }  
  13. if (foo < -9) {  
  14.     console.log('less than -9');  
  15. }  
  16. if (foo == 0) {  
  17.     console.log('equal to zero');  
  18. }  
  19. if (foo === 0) {  
  20.     console.log('equal to zero');  
  21. }   

You will get the output as shown below.

JavaScript

You can see that JavaScript is treating null as zero for the comparison operations.

JavaScript

What is undefined?

In JavaScript, undefined is created at run time and assigned as a missing value. Therefore, if you have not assigned any value to a variable, then at run time, JavaScript will assign undefined to that variable.

Consider the code given below.

  1. var foo;  
  2. console.log(foo);   

Since you are not assigning any value to foo, at runtime, JavaScript will assign undefined to foo. The output of the above code listing would be as shown in the image below.

JavaScript

Therefore, in JavaScript, undefined represents a missing value.

JavaScript assigns undefined in the following scenarios.

  1. When a variable is declared but not assigned any value.
    1. var foo;  
    2. console.log(foo);  
  1. When a function parameter is missing. So, in the below code, you are not passing value of parameter b, therefor JavaScript will assign value undefined to b.
    1. function foo(a, b) {  
    2.     console.log(b);  
    3. }  
    4.   
    5. var res = foo(9);   

    Output of above code listing would be “undefined” as shown in the image below.

    JavaScript
  1. When you call a function as function invocation pattern and if explicitly function does not return any value, then it returns undefined.

    JavaScript 
    1. function foo(a, b) {  
    2.   
    3.     var res = a + b;  
    4. }  
    5. var r = foo(8, 9);  
    6. console.log(r);   

    In the above listing, you are calling a function as function invocation pattern. In FIP, when function does not return anything explicitly, JavaScript returns undefined for that. Therefore, output of the above function would be as shown in the image below.

    JavaScript
  1. JavaScript also assigns undefined to out of array index in an array.
    1. var foo = [5, 1, 6, 9];  
    2. console.log(foo[4]);   

    In the above code snippet, you are trying to print the fifth element of the array, however the size of the array is four. Therefore, JavaScript will print undefined for the fifth element. JavaScript will give you output as shown in the image below,

    JavaScript
  1. JavaScript returns undefined on accessing the object property, which does not exist. Let us consider below listed code
    1. Student = {  
    2.   
    3.     name: 'foo',  
    4.     age: 9  
    5. }  
    6.   
    7. console.log(Student.grade);  

Student object does not have grade property, so JavaScript will print undefined for this. You will get the below output for the above code,

JavaScript

In arithmetic, for operations if one of the operand is undefined then JavaScript will always evaluate result to NaN. Consider the code listing given below, 

  1. var foo;  
  2. var a = foo + 9;  
  3. var b = foo - 9;  
  4. var c = foo * 9;  
  5. var d = foo % 9;  
  6. console.log(a);  
  7. console.log(b);  
  8. console.log(c);  
  9. console.log(d);   

As output, JavaScript will evaluate all operations to NaN as shown in the image below,

JavaScript

Therefore, if any of the operands are undefined then JavaScript evaluates arithmetic operation to NaN.

JavaScript

Type of null was object, however, type of undefined is a special type undefined.

  1. console.log(typeof (undefined));   

Above code will print type of undefined as undefined.

JavaScript

null and undefined

In strict comparison, null and undefined are not same. Therefore, the below code will give you output as false.

  1. console.log(null === undefined);   

However, in type-converting equality comparison null and undefined are same. Therefore, below code will give you output true

  1. console.log(null == undefined);   

In this post, you learned one of the most important concepts of JavaScript - undefined and null. Having a good understanding of undefined and null helps you in faster debugging and writing better JavaScript codes. I hope, now, you will able to answer undefined and null in the interviews.