Type of Undeclared Variable in JavaScript: What is it?

 

Have you ever thought, what is type of undeclared variable in JavaScript? I know, the first thing that might come to mind is: how can an undeclared variable have a type? Yes, in JavaScript it is possible.

To understand it, let us start with understanding types in JavaScript. There are seven built in types in JavaScript. They are as follows:

  1. null
  2. undefined
  3. boolean
  4. number
  5. string
  6. object
  7. symbol (added on ES6)

Each variable with assigned value has a type. Let us consider the code listed below:

As you can see in the above snippet, if there is no value assigned then type of variable is undefined.

So far so good, we saw that variable with no assigned value is undefined.  Let us consider the next code snippet:

We have created a variable koo and have not assigned any value to it.  Now both value and type of koo are set to undefined.

Read full article on the Infragistics blog

JavaScript Interview Day # 2: Can you explain undefined and null

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.

Get it right: null and undefined in JavaScript

Well, whenever I talk to developers, I find them in confusion about null and undefined in JavaScript. In this post I try to simplify understanding of null and undefined in JavaScript.

To start with null is language keyword and undefined in value of a variable. Both evaluates to value that indicates absence of a value.

To understand it better we can say null indicates an object with no value. Let us consider following code on null,


var result = typeof (null);
 console.log(result);

This will return you output as object. So to conclude we can say null is an object which contains no value or represents no object.

On the other hand undefined represents absence of value in a variable. Undefined is value of a variable. You get undefined value for a variable in following scenario

  • When variable is declared but not initialized
  • When function does not return a value
  • As value of function parameter which is not being passed
  • On accessing Array with index which does not exist
  • When accessing object property which does not exist

Let us consider following code on undefined,


var result = typeof (undefined);
 console.log(result);

This will return you output as undefined.

Other important difference is you cannot assigned undefined to a variable, whereas you can assign null to a variable. Undefined is read only.

Let us consider following code. It indicates that null and undefined represents same value that is absence of value but both are not same.


if (null == undefined) {
 console.log("both indicates absense of value");
 }

if (null === undefined) {
 console.log("both are same");
 }
 else {

console.log("both are not same");
 }

In last I will conclude that both undefined and null are falsy value in JavaScript. Both does not contain any property or methods.

I hope now you have better understanding on undefined and null in JavaScript. Thanks for reading.