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

Let us start by looking at code below:

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

image

What is going on here? Whenever you have created a variable in JavaScript and have not assigned any value to it, then that variable will contain a value “undefined”. So undefined is missing value in the JavaScript.

Let us consider another code snippet,

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

image

So what is going on here? When you create a variable but not sure what should be the value, you may assign it with the value null. Therefore, null is a primitive value in the 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

image

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 allow you to perform the task shown in the listing below:

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

You will get below output which shown type of null is object.

image

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

image

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

Output of above operation would be as below:

image

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

image

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

You will get output as shown below:

image

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

image

What is undefined?

In JavaScript undefined is created at run time and assigned as 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:

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

image

Therefore, in JavaScript undefined represents a missing value.

JavaScript assigns undefined in following scenarios:

1. When a variable is declared but not assigned any value

2. When a function parameter is missing. So in below code listing, you are not passing value of parameter b, therefor JavaScript will assign value undefined to b.

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

image

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

image

In 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 above function would be as shown in the image below:

image

4. JavaScript also assigns undefined to out of array index in an array.

In above code snippet, you are trying to print 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:

image

5. JavaScript returns undefined on accessing the object property, which does not exist. Let us consider below listed code

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

image

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

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

image

Therefore, if any of the operand is undefined then JavaScript evaluates arithmetic operation to NaN.

image

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

Above code will print type of undefined as undefined.

image

null and undefined

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

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

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

Advertisements

3 thoughts on “JavaScript Interview Day # 2: Can you explain undefined and null

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s