JavaScript

Explain the difference between “undefined” and “not defined” in JavaScript

Knowing the difference between “undefined” and “not defined” is considered an essential concept for learning JavaScript programming language. In JavaScript, “undefined” and “not defined” are the two separate terms related to memory space. The keyword “undefined” means there is a variable that is defined and contains space in memory without assigning value. While “not defined” means the variable is not yet defined.

In this article, we will learn the difference between undefined and not defined in JavaScript with the help of examples. So, let’s get started!

What is the “undefined” keyword in JavaScript?

The keyword “undefined” indicates that the accessed variable is declared in the program; however, we have not assigned any value to it.

When a JavaScript program is executed, memory is allocated to the declared or defined variables according to the execution context. For instance, in the following example, when the variable “emp_name” is defined in the program, it gets space in memory.

let emp_name;
console.log(emp_name);

As no value is assigned to the variable “emp_name”, the program will print “undefined” on the console

Now, we will assign the value “John” to the variable “emp_name” and try to display the specified value as output:

emp_name= "John";
console.log(emp_name);

Hence “emp_name” is no longer “undefined”, so the “console.log()” method will print “John” as its value:

Now, let’s move ahead to know about the “not defined” keyword.

What is the “not defined” keyword in JavaScript?

The keyword “not defined” indicates that the accessed variable does not exist in the memory. So, when we access a variable that is not declared in the program, it will print “not defined” on the console.

For instance, in the following example, we will access the variable “emp_age” that is not declared in the program:

console.log(emp_age);

The program will print “not defined” on the console:

We have provided essential information related to “undefined” and “not defined” JavaScript keywords.

Conclusion

In JavaScript, the main difference between “undefined” and “not defined” is declaration and initialization. The keyword “undefined” means the variable is declared but not assigned or initialized any value. While the keyword “not defined” means the variable is not yet declared. This blog discussed the difference between undefined and not defined JavaScript keywords with examples.

About the author

Sharqa Hameed

I am a Linux enthusiast, I love to read Every Linux blog on the internet. I hold masters degree in computer science and am passionate about learning and teaching.