JavaScript For Java Developers

Scopes and hoisting for variables and functions.

The topic contains problems related to:

  • Code Block;
  • Variable Scope;
  • Variable hoisting;
  • Function hoisting;

Set of the problems:

Javascript Variable Scope.

There is typically one global scope, and each function defined has its own (nested) local scope. Any function defined within another function has a local scope which is linked to the outer function.

Statement aVar=10; (without var) creates variable in a global scope (of course if variable was not defined before).

Block scope (grouped statements with curly braces { ... } ) created by if, for, while, switch and other statements does not define a new scope. Only function has it's own scope.

Links to read:

Javascript hoisting.

Variable and funtion declarations are processed before any code is executed.

For variable gets hoisted declaration only. Value would be "undefined".

Function gets hoisted completely (name and implementation).

Links to read: