Difference Between Scoping and Closures

In easy words,

Table of Contents

Closure

Closure is when a function reads or writes a variable that is declared outside of it.

If you declare any inner function within another function, the inner function is called closure.

It can access the variables created in the outer function or let’s say the main function.

Scoping

When you’re declaring a variable inside a function, it is ONLY accessible WITHIN or INSIDE the function. Not to the outer elements. This is called scoping a function.

Block-Scope

if (true) {
	const hmmOkay = "Hi";
	console.log(hmmOkay); // "Hi"
}

We won’t be able to access hmmOkay outside this function.

This is called a Block Scope

Leave a Reply

Your email address will not be published. Required fields are marked *