Scope is the area or location within the program where a variable or function is accessible.

Lexical also called static Scoping defines how variable names are resolved in nested functions: inner functions contain the scope of parent functions even if the parent function has returned.

 
 var scope = "I am global scope";
 function whatismyscope(){
    var scope = "I am local scope";
    
    function getScope() { return scope; }
    
    return getScope;
 }
 whatismyscope()() 
 

The above code will return “I am local scope“. It will not return “I am a global“. Because the function getScope () counts where is was originally defined which is under the scope of function whatismyscope.

It will not bother from whatever it is being called (the global scope/from within another function even), that’s why global scope value I am global will not be printed.

IBM defines it as:


The portion of a program or segment unit in which a declaration applies. An identifier declared in a routine is known within that routine and within all nested routines. If a nested routine declares an item with the same name, the outer item is not available in the nested routine.


For more detail: 

http://stackoverflow.com/questions/1047454/what-is-lexical-scope
http://pierrespring.com/2010/05/11/function-scope-and-lexical-scoping/


Leave a Reply

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

Related Posts

HTML

HTML : Properties and Attributes

Properties and attributes of a DOM element seem same but there is a difference between these two terms. When writing HTML source code, attributes are defined in HTML elements. Browser parses HTML code and a corresponding Read more…

Interview Questions

Nodejs Interview Questions

Important Interview Questions for Node.js 1) What is Nodejs ? 2) What is NPM ? 3) Different types of API’s exist in Node (api methods)? 4) What is event looping ? 5) What is event Read more…

Javascript

console.log : Stylize console logging in browser

console.log() method logs any string to console window of the browser. But you know, there is a way to provide style to string message even we can draw images 😉 like in facebook prints in console window   Read more…