Hoisting - Why does JavaScript hoist variables

De openkb
Aller à : Navigation, rechercher

Sommaire

Questions

  Why does JavaScript hoist variables?   

What gain was so important to the designers of the language that they decided to implement hoisting? I can t think of any other popular language which does this.

Answers

As Stoyan Stefanov explains in "JavaScript Patterns" book, the hoisting is result of JavaScript interpreter implementation.

The JS code interpretation performed in two passes. During the first pass, the interpreter processes variable and function declarations.

The second pass is actually code execution step. The interpreter processes function expressions and undeclared variables.

Thus, we can use the "hoisting" concept to describe such behavior.

Hope this helps.

Source

License : cc by-sa 3.0

http://stackoverflow.com/questions/15005098/why-does-javascript-hoist-variables

Related

Outils personnels
Espaces de noms

Variantes
Actions
Navigation
Outils