From 8737aa9e4de3684255393174bdec503bb774171d Mon Sep 17 00:00:00 2001 From: Anusha-Pwr Date: Sun, 21 Jun 2026 23:01:24 +0530 Subject: [PATCH 1/3] Add example of var declaration with a same-name function --- .../javascript/reference/statements/var/index.md | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/files/en-us/web/javascript/reference/statements/var/index.md b/files/en-us/web/javascript/reference/statements/var/index.md index bd0b93698542c1e..1aa1be77325b120 100644 --- a/files/en-us/web/javascript/reference/statements/var/index.md +++ b/files/en-us/web/javascript/reference/statements/var/index.md @@ -150,6 +150,19 @@ function a() {} console.log(a); // 1 ``` +A `var` declaration without an initializer does not overwrite the function's value. + +```js +function foo() { + console.log(typeof a); // "function" + var a; + function a() {} + console.log(typeof a); // "function" +} + +foo(); +``` + `var` declarations cannot be in the same scope as a {{jsxref("Statements/let", "let")}}, {{jsxref("Statements/const", "const")}}, {{jsxref("Statements/class", "class")}}, or {{jsxref("Statements/import", "import")}} declaration. ```js-nolint example-bad From a117cd5a17e1a7e41e869223ec50245ac83f58b5 Mon Sep 17 00:00:00 2001 From: Joshua Chen Date: Sun, 12 Jul 2026 12:49:23 -0700 Subject: [PATCH 2/3] Update index.md --- .../reference/statements/var/index.md | 20 +++++++------------ 1 file changed, 7 insertions(+), 13 deletions(-) diff --git a/files/en-us/web/javascript/reference/statements/var/index.md b/files/en-us/web/javascript/reference/statements/var/index.md index 1aa1be77325b120..c150a47be6ebdbd 100644 --- a/files/en-us/web/javascript/reference/statements/var/index.md +++ b/files/en-us/web/javascript/reference/statements/var/index.md @@ -142,25 +142,19 @@ var a; console.log(a); // 2; not undefined ``` -`var` declarations can also be in the same scope as a `function` declaration. In this case, the `var` declaration's initializer always overrides the function's value, regardless of their relative position. This is because function declarations are hoisted before any initializer gets evaluated, so the initializer comes later and overrides the value. +`var` declarations can also be in the same scope as a `function` declaration. Both the function declaration and the `var` declaration are hoisted to the top, so the function value is only accessible from the start of its scope until the variable's initializer or first assignment, regardless of the two declarations' relative positions in the source code. ```js -var a = 1; +console.log(typeof a); // "function" +var a; function a() {} -console.log(a); // 1 +console.log(typeof a); // "function" ``` -A `var` declaration without an initializer does not overwrite the function's value. - ```js -function foo() { - console.log(typeof a); // "function" - var a; - function a() {} - console.log(typeof a); // "function" -} - -foo(); +var a = 1; +function a() {} +console.log(a); // 1 ``` `var` declarations cannot be in the same scope as a {{jsxref("Statements/let", "let")}}, {{jsxref("Statements/const", "const")}}, {{jsxref("Statements/class", "class")}}, or {{jsxref("Statements/import", "import")}} declaration. From 08889bf8a7e94798a6b4263a934e1393bab73f56 Mon Sep 17 00:00:00 2001 From: Joshua Chen Date: Sun, 12 Jul 2026 12:50:37 -0700 Subject: [PATCH 3/3] Update index.md --- files/en-us/web/javascript/reference/statements/var/index.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/files/en-us/web/javascript/reference/statements/var/index.md b/files/en-us/web/javascript/reference/statements/var/index.md index c150a47be6ebdbd..7a401280ebcf11a 100644 --- a/files/en-us/web/javascript/reference/statements/var/index.md +++ b/files/en-us/web/javascript/reference/statements/var/index.md @@ -146,8 +146,8 @@ console.log(a); // 2; not undefined ```js console.log(typeof a); // "function" -var a; function a() {} +var a; console.log(typeof a); // "function" ```