JavaScript Event

JavaScript Scope

JavaScript Scope refers to the variables that are available to a piece of code at a given time. A lack of understanding of scope can lead to frustrating debugging experiences. When a variable is declared inside of a function using the var keyword, it is only available to code inside of that function — code outside of that function cannot access the variable. On the other hand, functions defined inside that function will have access to to the declared variable.

Furthermore, variables that are declared inside function without the var keyword are not local to the JavaScript Function — JavaScript will traverse the scope chain all the way up to the window scope to find where the variable was previously defined. If the variable wasn’t previously defined, it will be defined in the global scope, which can have extremely unexpected consequences;

Functions have access to variables defined in the same scope


var foo = ’ hello ’;
var say Hello = function () {
console . log ( foo );
};
say Hello (); // logs ’ hello ’
console. log ( foo ); // also logs ’ hello ’

Code outside the scope in which a variable was defined does not have access to the variable

var say Hello = function () {
var foo = ’ hello ’;
console . log ( foo );
};
say Hello (); // logs ’ hello ’
console. log ( foo ); // doesn’ t log anything

Variables with the same name can exist in different scopes with different values

var foo = ’ world ’;
var say Hello = function () {
var foo = ’ hello ’;
console. log ( foo );
};
sauy Hello (); // logs ’ hello ’
console. log ( foo ); // logs ’ world ’

Functions can “see” changes in variable values after the function is defined

var my function = function () {
var foo = ’ hello ’;
var myFn = function () {
console. log ( foo );
};
foo = ’ world ’;
return myFn ;
};
var f = my Function ();
f (); // logs ’ world ’ -- uh oh

JavaScript Scope insanity

// a self - executinganonymousfuction
( function () {
var baz = 1;
var bim = function () { alert ( baz ); };
bar = function () { alert ( baz ); };
} ) ( ) ;
c o n s o l e . log ( baz ); // baz is not defined outside of the function
bar (); // bar is defined outside of the anonymous function
// because it wasn’t ’ t declared with var ; furthermore ,
// b e c a u s e it was d e f i n e d in the same s c o p e as baz ,
// it has access to baz event though other code
// outside of the function does not
bim (); // bim is not defined ou t side of the anonymous function,
// so this will result in error

JavaScript Scope refers to the current context of code, which determines the accessibility of variables to JavaScript.

 

Online Training Tutorials

  • JavaScript EventJavaScript EventJavaScript Event : Imagine an interface where the only way to find out whether a key on the keyboard is being pressed is to read the current state of that key. To be able to react to […]
  • JavaScript Node.jsJavaScript Node.jsJavaScript Node.js, a program that allows you to apply your JavaScript skills outside of the browser. With it, you can build anything from small command line tools to HTTP servers that […]
  • JavaScript “this” KeywordJavaScript “this” KeywordIn JavaScript "this" Keyword, as in most object-oriented programming languages, this is a special keyword that is used within methods to refer to the object on which a method is being […]
  • JavaScript FunctionJavaScript FunctionThe definition expression defines a JavaScript function, and the value of such an expression is the newly defined function. In a sense, a function definition expression is a “function […]
  • JavaScript User InputJavaScript User InputJavaScript User Input : Clicks are powerful and easy and you can add an on Click event to pretty much any HTML element, but sometimes you need to be able to ask for input from the user and […]
  • JavaScript BooleanJavaScript BooleanJavaScript Boolean, It is often useful to have a value that distinguishes between only two possibilities, like “yes” and “no” or “on” and “off”. For this purpose, JavaScript has Boolean […]
  • JavaScript Interview Questions and AnswersJavaScript Interview Questions and AnswersJavascript is an interpreted language with a C like syntax. While many people brush the language off as nothing more than a browser scripting language, it actually supports many advanced […]
  • JavaScript Tutorial for Beginners –Learn JavaScript OnlineJavaScript Tutorial for Beginners –Learn JavaScript OnlineJavaScript Tutorial provides the basic and advanced concepts of JavaScript and this Tutorial is designed for beginners and professionals to learn JavaScript Tutorial Online. JavaScript is […]