Difference Between Scoping and Closures

In easy words,

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

Facebook
Twitter
LinkedIn
Pinterest

Table of Contents

Related posts