Undefined vs not defined in Javascript

While running code in javascript you might have seen errors like undefined, not defined, etc, and would have got confused by seeing them. To overcome this challenge we will be discussing undefined, not defined, and null in javascript so that you can get a good understanding of it.

Other than that the best part about these errors is that in a javascript interview they are asked a lot of times to test your basic knowledge about javascript.

Undefined

Undefined is a very special keyword in javascript and it is not there in other languages. Other than that it has a lot to do with how the javascript code is executed. In general, javascript assigns undefined to any variable that has been declared but not been defined or initialized. In simple words, if any value is not been explicitly assigned to a variable then javascript calls it undefined.

Let us understand it with the help of simple code snippets where we get the value as undefined.

Example: 

Undefined

Output :

Undefined Output

In the first example, we are trying to access the value of a which is not yet declared and assigned with some value so javascript considers it to be undefined until and unless a is declared and assigned with some value.

In the second example, we have declared a variable without assigning any value to it so javascript implicitly assigns its value as undefined.

In the third example, we are calling a  function multiply which doesn’t have anything to be done inside its body so the output is undefined.

In the fourth and fifth examples, we are trying to access the value of an array and object which is yet not assigned with the value so the output is undefined.

Not Defined 

Not defined error generally occurs in javascript when we don’t declare any variable but we try to get the value of that variable.

Example : 

not defined

Output :

Not Defined Output

In the above example, we are trying to log the value of the variable c which is yet not defined in this program. This is the reason why this program throws an error ReferenceError: c is not defined.

The best part about javascript is that we can declare variables without adding const, let, or var and we won’t get any error like undefined or not defined. Let us demonstrate it with the help of an example :

Example : 

undefined vs not defined

Output : 

Not Defined Output

Now let’s try to see another example with a slight variation. In this example will assign the value to the variable inside the function block and we will get not defined error for that.

Example : 

not defined example in javascript

Output :

Not Defined Output

In the above example, we can get the error that the value is not defined. This is because we didn’t define it as a datatype of let, const, and var. So the variable ‘value’ doesn’t have a space in the memory and we cannot assign a value to the variable.

This error can easily be solved by using declaring value with let. Hence through this, we give the value variable space in the memory to be able to assign values.

Conclusion

This was a complete explanation about some of the common errors in javascript like undefined and not defined. Hopefully, by reading this article I am sure you might have got a deep understanding of how to deal with undefined and not defined in javascript. The final takeaways we can take from this article are described below:
  • Undefined is a very special keyword in javascript and it is not there in other languages.
  • Javascript assigns undefined to any variable that has been declared but not been defined or initialized.
  • Not defined error generally occurs in javascript when we don’t declare any variable but we try to get the value of that variable.

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Stay Connected

0FansLike
3,586FollowersFollow
19,400SubscribersSubscribe
- Advertisement -spot_img

Latest Articles