Skip to content

Commit 1d746f4

Browse files
contents: improve hoisting concept answer (#6)
Co-authored-by: Yangshun Tay <[email protected]>
1 parent 5255c4c commit 1d746f4

File tree

1 file changed

+5
-1
lines changed

1 file changed

+5
-1
lines changed

questions/explain-hoisting/en-US.mdx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ title: Explain the concept of "hoisting" in JavaScript
44

55
## TL;DR
66

7+
Hoisting is a JavaScript mechanism where variable and function declarations are moved ("hoisted") to the top of their containing scope during the compile phase.
8+
79
- **Variable declarations (`var`)**: Declarations are hoisted, but not initializations. The value of the variable is `undefined` if accessed before initialization.
810
- **Variable declarations (`let` and `const`)**: Declarations are hoisted, but not initialized. Accessing them results in `ReferenceError` until the actual declaration is encountered.
911
- **Function expressions (`var`)**: Declarations are hoisted, but not initializations. The value of the variable is `undefined` if accessed before initialization.
@@ -27,7 +29,9 @@ The following behavior summarizes the result of accessing the variables before t
2729

2830
## Hoisting
2931

30-
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 their containing scope during compilation, which we refer to as hoisting.
32+
Hoisting is a term used to explain the behavior of variable declarations in JavaScript code.
33+
34+
Variables declared or initialized with the `var` keyword will have their declaration "moved" up to the top of their containing scope during compilation, which we refer to as hoisting.
3135

3236
Only the declaration is hoisted, the initialization/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 variables and their scopes, but it is easier to understand this behavior by visualizing the declarations as being "hoisted" to the top of their scope.
3337

0 commit comments

Comments
 (0)