KBKPU8FR9T

Global execution scope for JavasScript in a web browser should be the window object. Global variables and functions are properties and methods of the window object. These functions and variables are destroyed when the browser is shutdown or the web page closed.

When a function is called, a scope chain of variables is created which gives you ordered access to all available variables and functions. Functions will always have the arguments variable at the front of the scope chain of variables. These variables and functions are placed on a stack so the innermost context is next, then the next context, and so on until it reaches the global context.

In JavaScript, functions can be declared within functions. In other words, functions can be nested, so scope becomes very important.

When an identifier is used, the scope chain is searched, returning the first reference, which would be the innermost scope context of the variable. If the identifier isn’t found, an error usually occurs. All variables created inside a function are destroyed when the function finishes execution.

The catch statement and the with statement always move to the front of the scope chain, but are destroyed immediately after execution.

In JavaScript, there are no block-level scopes. This means that variables defined within a block statement (such as an if...then or for statement blocks) will also be accessible outside the block. It also means the variable will still be around after the block statement execution because the variables aren’t destroyed after execution like they are with functions.

Variables used inside functions without a declaration will not be destroyed.


function myfunc(x) {
  num = x*2;
}

myfunc(10);

alert(num);  //20

Changing that code by adding a var num = x*2 inside the function will give an error when num is accessed outside the function.

Leave a Comment

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

Scroll to Top