Clarify potential misunderstanding about hoisting

This commit is contained in:
Yangshun Tay 2018-04-15 16:58:39 -07:00
parent 4e11eeb22f
commit bca94516f4
1 changed files with 3 additions and 1 deletions

View File

@ -488,7 +488,9 @@ However, do be aware of a potential XSS in the above approach as the contents ar
### Explain "hoisting".
Hoisting is a term used to explain the behavior of variable declarations in your code. Variables declared or initialized with the `var` keyword will have their declaration "hoisted" up to the top of the current scope. However, only the declaration is hoisted, the assignment (if there is one), will stay where it is. Let's explain with a few examples.
Hoisting is a term used to explain the behavior of variable declarations in your code. Variables declared or initialized with the `var` keyword will have their declaration "moved" up to the top of the current scope, which we refer to as hoisting. However, only the declaration is hoisted, the assignment (if there is one), will stay where it is.
Note that the declaration is not actually moved - the JavaScript engine parses the declarations during compilation and becomes aware of declarations and their scopes. It is just easier to understand this behavior by visualizing the declarations as being hoisted to the top of their scope. Let's explain with a few examples.
```js
// var declarations are hoisted.