Home >Web Front-end >JS Tutorial >What is internal scope in js
Inner scope is the creation of a local scope in JavaScript, limiting variable access to only the function or block to which it belongs, thereby encapsulating and protecting data, improving maintainability, and optimizing memory management. Creation methods include: 1. Declare variables in functions; 2. Use curly braces to define scopes in block statements.
Inner Scope
In JavaScript, an inner scope is one created within a function or block statement local scope. It gives a private scope to the declaration inside a function or block, making it inaccessible from outside.
How to create an inner scope?
In JavaScript, you can use the following methods to create internal scopes:
Purposes of inner scope
Inner scope has the following purposes:
Example
The following example demonstrates inner scope:
<code class="javascript">function outerFunction() { var outerVariable = "Outer Variable"; function innerFunction() { var innerVariable = "Inner Variable"; console.log(outerVariable); // "Outer Variable" console.log(innerVariable); // "Inner Variable" } innerFunction(); } outerFunction();</code>
In this example, outerFunction
creates creates an inner scope, and innerFunction
creates another inner scope. outerVariable
is visible in both scopes, while innerVariable
is only visible in innerFunction
.
The above is the detailed content of What is internal scope in js. For more information, please follow other related articles on the PHP Chinese website!