From 0610db92f6d4aa72105d67c53978b6a47717b0c6 Mon Sep 17 00:00:00 2001 From: Sneha <144430030+SnehaReddy2025@users.noreply.github.com> Date: Wed, 4 Dec 2024 12:05:25 +0530 Subject: [PATCH 1/2] Update en-US.mdx --- questions/explain-hoisting/en-US.mdx | 1 + 1 file changed, 1 insertion(+) diff --git a/questions/explain-hoisting/en-US.mdx b/questions/explain-hoisting/en-US.mdx index e664d2e..6bd2f92 100644 --- a/questions/explain-hoisting/en-US.mdx +++ b/questions/explain-hoisting/en-US.mdx @@ -26,6 +26,7 @@ The following behavior summarizes the result of accessing the variables before t --- ## Hoisting +How to Answer In interview: Hoisting is a JavaScript mechanism where variable and function declarations are moved, or "hoisted," to the top of their containing scope during the compile phase. 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. From 7f734cdf8de334922181abd3d46d669c0f596f48 Mon Sep 17 00:00:00 2001 From: Yangshun Tay Date: Wed, 8 Jan 2025 10:17:22 +0800 Subject: [PATCH 2/2] Update en-US.mdx --- questions/explain-hoisting/en-US.mdx | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/questions/explain-hoisting/en-US.mdx b/questions/explain-hoisting/en-US.mdx index 6bd2f92..edf3b8b 100644 --- a/questions/explain-hoisting/en-US.mdx +++ b/questions/explain-hoisting/en-US.mdx @@ -4,6 +4,8 @@ title: Explain the concept of "hoisting" in JavaScript ## TL;DR +Hoisting is a JavaScript mechanism where variable and function declarations are moved ("hoisted") to the top of their containing scope during the compile phase. + - **Variable declarations (`var`)**: Declarations are hoisted, but not initializations. The value of the variable is `undefined` if accessed before initialization. - **Variable declarations (`let` and `const`)**: Declarations are hoisted, but not initialized. Accessing them results in `ReferenceError` until the actual declaration is encountered. - **Function expressions (`var`)**: Declarations are hoisted, but not initializations. The value of the variable is `undefined` if accessed before initialization. @@ -26,9 +28,10 @@ The following behavior summarizes the result of accessing the variables before t --- ## Hoisting -How to Answer In interview: Hoisting is a JavaScript mechanism where variable and function declarations are moved, or "hoisted," to the top of their containing scope during the compile phase. -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. +Hoisting is a term used to explain the behavior of variable declarations in JavaScript 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. 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.