Skip to main content

JavaScript Scopes

 What is a variable scope?

The visibility and accessibility of a variable is determined by its scope. JavaScript has three scopes:

  • Global scope
  • Local scope and
  • Block scope

Global Scope

A variable that can be accessible everywhere in the script. 

Local Scope   

If you declare a variable inside a function are local to the function. They are called local variables.

Output: 

I am from say
I am from global




Scope Chain

If interpreter cannot find a variable in its local context, then it looks for it in parent contexts until it's found. This is called scope chain.

Output: 
Hello I am message




In the above script, the JavaScript performs the following:
  • It looks up for the variable message in the current context (Functional Execution Context) of the say(). But it cannot find it.
  • So it goes out of the function and looks in the outer execution context which is the global execution context. It found the message variable there.
  • The way that JavaScript resolves a variable is by looking at in its current scope, if it cannot find the variable then it goes to the outer scope. This process is a Scope Chain.

Block Scope

ES6 provides the let and const keywords that allow you to declare variables in a block scope.
Generally whenever you see curly braces { }, it is a block. It can be the area within the if else, switch conditions or for , do while and while loops.
let is block scoped, var is function scoped.

Comments

Popular posts from this blog

JavaScript Runtime Environment

 What is a Runtime Environment? A runtime environment is where your program will be executed. It determines what global objects your program can access and it can also impact how it runs. How does the JavaScript Environment Work? The JavaScript runtime environment provides access to built-in libraries and objects that are available to program so that it can interact with the outside world and make the code work. Different browsers have different JavaScript Runtime Environments. In the context of a browser, JS runtime  is comprised of the following elements: The JavaScript Engine Web APIs The Call Back Queue The Event Loop The JavaScript Engine All the browsers have a JavaScript Engine. But all are not necessarily the same engine. Also, all the engines follow the same standard although their implementation is different. For example, Google Chrome has the V8 engine and Mozilla Firefox has Spyder Monkey as a JavaScript engine. The JavaScript Engine parses the code for us. It has ...

JavaScript Execution Context

 Consider the following script, How is the above script executed? The execution steps are as follows: First, assign 100 to variable x. Assign a value of 50 to variable y. Declare a function addNums, it adds the numbers passed as arguments. Call the addNums() function by passing in x and y as parameters, and store the return value in the result variable. Log the result to the console. Internally the JavaScript engine executes by creating execution contexts. What is JavaScript Execution Context? When a JavaScript engine executes a script, it creates contexts. Each execution context has two phases : Creation Phase and  Execution Phase   The Creation Phase When a script executes for the first time, the JavaScript engine creates a global execution context. During this creation phase, it performs the following tasks: Create a global object (window for browser and global in NodeJs env) Create  a " this " object which points to the global object Setup a memory heap for...