HTML Diff
0 added 0 removed
Original 2026-01-01
Modified 2026-03-09
1 <p>The local scope is limited by the function, so we cannot obtain function variables from outside of it. However, everything works differently from inside the function as opposed to outside of it.</p>
1 <p>The local scope is limited by the function, so we cannot obtain function variables from outside of it. However, everything works differently from inside the function as opposed to outside of it.</p>
2 <p>If we call some<b>non-</b>local variable from inside the function, JavaScript will move one level higher and search outside the scope of the function to find the desired variable. It is a known principle that<i>variables are read from inside the current scope to the outside</i>.</p>
2 <p>If we call some<b>non-</b>local variable from inside the function, JavaScript will move one level higher and search outside the scope of the function to find the desired variable. It is a known principle that<i>variables are read from inside the current scope to the outside</i>.</p>
3 var food = 'salad'; var eatDinner = function () { // The local variables are not declared inside the function. console.log('I ate ' + food); }; eatDinner(); // The function calls the food variable, // which is declared outside of eatDinner // Outputs: I ate salad.<p>In our example, the food variable is <b>not</b>declared inside a particular function. It is declared at the level of the entire program, which means that it can be seen from everywhere. In other words, it can be used inside every function. These variables, which are declared at the very top level, outside of all particular functions, are called<i>global</i>. And the scope in which they can be found is called the<i>global scope</i>. We can tell from their name that the variables from this scope can be seen throughout the entire program code, and they are available in all blocks of code.</p>
3 var food = 'salad'; var eatDinner = function () { // The local variables are not declared inside the function. console.log('I ate ' + food); }; eatDinner(); // The function calls the food variable, // which is declared outside of eatDinner // Outputs: I ate salad.<p>In our example, the food variable is <b>not</b>declared inside a particular function. It is declared at the level of the entire program, which means that it can be seen from everywhere. In other words, it can be used inside every function. These variables, which are declared at the very top level, outside of all particular functions, are called<i>global</i>. And the scope in which they can be found is called the<i>global scope</i>. We can tell from their name that the variables from this scope can be seen throughout the entire program code, and they are available in all blocks of code.</p>
4 <p>If a variable is called from inside a function, but this variable has not been declared in any scope, the console will output an error.</p>
4 <p>If a variable is called from inside a function, but this variable has not been declared in any scope, the console will output an error.</p>