What is hoisting in JavaScript? What is the difference between undefined and not defined?

Shubhada Beldar
3 min readJan 25, 2021

--

Java is to JavaScript as Gulab is to Gulabjamun!!!!

This was the statement I came across which was written by Akshay Saini Sir
And I became interested in knowing JavaScript and went through the entire series of Namaste JavaScript!!!

I had lots of questions in my mind and this series cleared all my doubts!!
So here, I am going to summarise the basic concepts of JavaScript which I learned from the videos!!!

This article covers a lot of concepts such as hoisting, the difference between undefined and not defined, What is the shortest JavaScript program, etc.

So let’s get started!!!

What is hoisting?

Hoisting is the ability to access variables and functions even before initializing them.

As I mentioned in my previous article titled “how is code in JavaScript executed” that even before code in JavaScript is executed, in the memory creation phase, memory is allocated to every variable along with a placeholder named “undefined”.

Example:

console.log(x)

var x;

It will give output as undefined because x is accessed before it is initialized.

This is one of the special features of JavaScript.

When we get Reference Error as Not Defined?

Ex: console.log(x)

Output is a Reference Error showing x is not defined because x is not present in the code and still you are trying to access the value.

Let us quickly revise undefined and not defined in a single piece of code.

console.log(a)

var a= 7;

console.log(a);

console.log(x);

Output :

undefined //Because value is accessed before initializing a

7//now a has assigned a value 7

Reference Error x is not defined//Because the value of x is not defined anywhere in the memory space.

Have you wondered what is the shortest JavaScript Program?

This is an empty JavaScript file(index.js) because in every file execution context is created and it sets up memory space.

It also creates a “Window”.

So what is a Window?

It is a big object with lots of functions and methods created by JavaScript in the global space. These can be accessed anywhere in the code.

Introduction to this keyword

Whenever you create an execution context, a “this” is also created along with it. This is applicable for functional and global execution contexts. At a global level, “this” points to the global object “window” in the case of the browser.

Example:

var a= 10

console.log(window.a)

console.log(a)

console.log (this.a)

Output:

10

10

10

In the global space window. a , this. a and a refer to the same value.

If you wish to see the video from where I got to know these concepts, you can check it on this video link in the Namaste JavaScript series by Akshay Sir.

Please share your feedback on this article and if you liked it share it will all your friends!!

In the upcoming article, I will be explaining the scope chain, scope, and lexical environment, temporal dead zone, and difference between syntax error, reference error, and type error.

Till then happy learning!!!

--

--

Shubhada Beldar
Shubhada Beldar

Written by Shubhada Beldar

I am a Software Engineer working in the domain of full stack web development .I love to share my thoughts, ideas and experiences with others through blogs!!!

No responses yet