Learn JavaScript
Scope
24.3
Lesson

Scope nesting

In JavaScript, we can define a function inside another function. The inner function can access the variables defined in the outer function.

However, the outer function does not have access to the variables defined in the inner function.

In the example given below, we have declared the variable outerNum inside outerFunc function. There’s another function innerFunc declared inside outerFunc. The inner function has a variable innerNum declared inside it.

We can access outerNum from both the outerFunc and the innerFunc function. But, we can access the innerNum only from inside the innerFunc function. If we try to access it from outerFunc, we get an error.