From bca94516f4a12961d505b3a658b3f18d095db735 Mon Sep 17 00:00:00 2001 From: Yangshun Tay Date: Sun, 15 Apr 2018 16:58:39 -0700 Subject: [PATCH] Clarify potential misunderstanding about hoisting --- questions/javascript-questions.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/questions/javascript-questions.md b/questions/javascript-questions.md index 6f3f8e581..7bc8201fa 100644 --- a/questions/javascript-questions.md +++ b/questions/javascript-questions.md @@ -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.