Scope (Local or Global)


Scope is a very important method of functions and java script, actully scope always deside that which part of function is can access by inside the functions or outside the functions. We can understand about scope by give a overview on functions.

  1. Local var/function: The variables or functions who creates into a function are called local variables or functions and we also can access them only from inside that functions where we make it.
  2. Global var/function: The varibales or functions who creates outside from the functions are called global functions or variables, we can access them from both inside and outside from the functions.

So which part of functions(local/global) access from where (inside/outside). Always can deside by Scopes

Java Identifier:

Scopes also takes help of java identifier looksup. Identifier is always search var's and than it deside that where a user can acces this var by inside or form outside of the functions.

Let Understand all by some Examples :

This is a best example to understand method of scopes.


var globalVar = "Hi there! I am a global variable, I can access by anywhere else.";
function globalFunction(){
localVar = "Hi there! I am a local variable, I can only access by inside the function.";
console.log(localVar);
console.log(globalVar);
globalVar = "Hi there! I'm global variable and now i have been modified.";
}
globalFunction();
console.log(globalVar);

Let see an example of local functions and global function:


var globalVar = "Hi there! I'm a global variable.";
function globalFunction(){
var localVar = "Hi there! I'm a local variable.";
function localFunction(){
var superLocalVar = "Hi there! I'm a super local variable."
console.log(superLocalVar);
};
localFunction();
console.log(localVar);
};
globalFunction();
console.log(globalVar);


Wconcert India © 2015   ·   All Rights Reserved

Free Web Hosting