1) Variables are visible for the whole function scope. Therefore, you should only declare them once.

2) You should not declare the variable twice in your example. I'd recommend declaring the variable at the top of the function, then just setting the value later:

function actionPane(state) {
    var structure;
    if(state === "ed") {
        structure = {    
            ...

For excellent feedback on JavaScript, I highly recommend using JSLint by Douglas Crockford. It will scan your code for common errors, and find suggestions for cleanup.

I also recommend reading the small book JavaScript: The Good Parts. It contains a lot of tips for writing maintainable JS code.

Answer from OverZealous on Stack Overflow
🌐
W3Schools
w3schools.com › js › js_scope.asp
JavaScript Scope
Variables declared Globally (outside any block or function) have Global Scope. Global variables can be accessed from anywhere in a JavaScript program.
🌐
Jack Franklin
jackfranklin.co.uk › blog › javascript-variable-scope-this
Scope and this in JavaScript - Jack Franklin
April 16, 2012 - The idea of "scope" is that it's where certain functions or variables are accessible from in our code, & the context in which they exist & are executed in.
Discussions

Are JavaScript variables declared in the body of an if statement scoped to the body? - Stack Overflow
It's not possible to declare a variable with either let or const and have its scope remain within a conditional if block. JavaScript has no "block scope", it only has function scope - so variables declared inside an if statement (or any conditional block) are "hoisted" to the outer scope. More on stackoverflow.com
🌐 stackoverflow.com
Javascript Variable Scope in 2.6 Minutes…

Any variable (var, let or const) declared outside of functions or any block of code (not within curly brackets { }) is global scope.

This isn't the case since modules also define their own, non-global scope. Any declarations outside of functions or blocks in a module are part of that module scope, not global.

More on reddit.com
🌐 r/learnjavascript
7
0
June 1, 2021
Why Javascript devs hate "var" function scoped variable declarations, when python devs are using function scoped variables without any problem?
As a Python developer, I hate it too. I'd rate it as one of the biggest warts in the language ( here's a bunch of other warts ). It bites me the most often when creating lambdas inside a loop: fns = [] for i in range(5): fns.append(lambda: i) for fn in fns: print(fn()) # Prints "5" 5 times. The "fix" is to use lambda i=i: i, so that it binds the value to a new locally scoped variable with the same name. The current scoping rules do have their advantages: for i in range(100): pass # Print the last loop value: print(i) But I'd still rather have block-scoped names. More on reddit.com
🌐 r/ProgrammingLanguages
62
54
December 1, 2021
How do I avoid polluting the global scope in JavaScript
Change your mental model. Instead of a function "accessing" variables, it should be passed variables. So the pseudocode for updateScore might be something like: function updateScore(currentScore) { const newScore = currentScore + 1; return newScore; } Then when you call it you'd do: score = updateScore(score); More on reddit.com
🌐 r/learnprogramming
7
58
June 18, 2022
🌐
SitePoint
sitepoint.com › blog › javascript › demystifying javascript variable scope and hoisting
Demystifying JavaScript Variable Scope and Hoisting — SitePoint
November 13, 2024 - Understanding and correctly utilizing ... scope of a variable is controlled by the location of the variable declaration, and it defines the part of the program where a particular variable is accessible....
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › javascript-scope
Scope of Variables in JavaScript - GeeksforGeeks
September 26, 2025 - In JavaScript, scope is the context that determines where variables can be accessed, helping write cleaner and error-free code.
Top answer
1 of 5
62

1) Variables are visible for the whole function scope. Therefore, you should only declare them once.

2) You should not declare the variable twice in your example. I'd recommend declaring the variable at the top of the function, then just setting the value later:

function actionPane(state) {
    var structure;
    if(state === "ed") {
        structure = {    
            ...

For excellent feedback on JavaScript, I highly recommend using JSLint by Douglas Crockford. It will scan your code for common errors, and find suggestions for cleanup.

I also recommend reading the small book JavaScript: The Good Parts. It contains a lot of tips for writing maintainable JS code.

2 of 5
56

NOTE: This answer is from 2011. It's not possible to declare a variable with either let or const and have its scope remain within a conditional if block.


JavaScript has no "block scope", it only has function scope - so variables declared inside an if statement (or any conditional block) are "hoisted" to the outer scope.

if(true) {
    var foo = "bar";
}
alert(foo); // "bar"

This actually paints a clearer picture (and comes up in interviews, from experience :)

var foo = "test";
if(true) {
    alert(foo); // Interviewer: "What does this alert?" Answer: "test"
    var foo = "bar";
}
alert(foo); // "bar" Interviewer: Why is that? Answer: Because JavaScript does not have block scope

Function scope, in JavaScript, typically refers to closures.

var bar = "heheheh";
var blah = (function() {
    var foo = "hello";
    alert(bar); // "heheheh"
    alert(foo); // "hello" (obviously)
});

blah(); // "heheheh", "hello"
alert(foo); // undefined, no alert

The inner scope of the function has access to the environment in which it is contained, but not the other way around.

To answer your second question, optimisation can be achieved by initially constructing a 'minimal' object which satisfies all conditions and then augmenting or modifying it based on particular condition(s) which has/have been satisfied.

🌐
Simplilearn
simplilearn.com › home › resources › software development › javascript tutorial: learn javascript from scratch › what is the scope of variables in javascript and its types
What Is the Scope of Variables in Javascript | Simplilearn
November 26, 2024 - Explore the scope of variabes in Javascript and its types in detail. Read on to know how the global, local, block and function scope types help in accessibility of variable.
Address   5851 Legacy Circle, 6th Floor, Plano, TX 75024 United States
Find elsewhere
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Glossary › Scope
Scope - Glossary | MDN
If a variable or expression is not in the current scope, it will not be available for use. Scopes can also be layered in a hierarchy, so that child scopes have access to parent scopes, but not vice versa. JavaScript has the following kinds of scopes: Global scope: The default scope for all code running in script mode.
🌐
Study.com
study.com › computer science courses › introduction to javascript
Variable Scope in JavaScript: Explanation & Examples | Study.com
The variable loses its scope outside of the function. Thus, the variable has a function or local scope. Local variables are always declared with the keyword ''var''. A local variable gets created when the JavaScript function begins and is deleted ...
🌐
Codecademy
codecademy.com › docs › javascript › variable scope
JavaScript | Variable Scope | Codecademy
February 3, 2023 - Variable Scope refers to the areas where variables are visible and accessible. Before ES6 was introduced in 2015, JavaScript used only global scope and function scope.
🌐
Programiz
programiz.com › javascript › variable-scope
JavaScript Variable Scope (with Examples)
In JavaScript, a variable declared outside any function or in the global scope is known as a global variable.
🌐
DEV Community
dev.to › micmath › javascript-variable-scope-by-example-3970
JavaScript Variable Scope by Example - DEV Community
May 30, 2025 - Knowing how var, let, and const ... predictable code. In JavaScript, scope refers to the current context of execution in which values and expressions are "visible" or accessible....
🌐
Reddit
reddit.com › r/learnjavascript › javascript variable scope in 2.6 minutes…
r/learnjavascript on Reddit: Javascript Variable Scope in 2.6 Minutes…
June 1, 2021 -

Here is a post about scoping and javascript variables. Differences in scope mean differences in accessibility and usability for the variables that we declare. I hope this post helps everyone learn or brush up on their knowledge when it comes to declaring variables. Please feel free to reach out with any questions, queries, comments or banter!

https://thecodingapprentice.substack.com/p/javascript-variable-scope-in-26-minutes?sd=pf

🌐
freeCodeCamp
freecodecamp.org › news › scope-in-javascript-global-vs-local-vs-block-scope
Scope in JavaScript – Global vs Local vs Block Scope Explained
September 4, 2024 - In JavaScript, global scope is the widest scope available. Variables declared in global scope are accessible from anywhere in your code, whether it's inside functions, conditional statements, loops, or other blocks of code.
🌐
Udacity
udacity.com › blog › 2021 › 06 › javascript-global-variable-scopes.html
Controlling Execution Contexts with Javascript Global Scope and Javascript Variable Scopes | Udacity
June 16, 2021 - The Javascript global scope is the context where everything in a Javascript program executes by default. This scope includes all variables, objects, and references that are not contained within a customized scope defined by a programmer.
🌐
GreatFrontEnd
greatfrontend.com › questions › quiz › explain-the-concept-of-scope-in-javascript
Explain the concept of scope in JavaScript | Quiz Interview Questions with Solutions
September 5, 2021 - In JavaScript, scope determines the accessibility of variables and functions at different parts of the code. There are three main types of scope: global scope, function scope, and block scope.
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Statements › var
var - JavaScript | MDN
The var statement declares function-scoped or globally-scoped variables, optionally initializing each to a value. var x = 1; if (x === 1) { var x = 2; console.log(x); // Expected output: 2 } console.log(x); // Expected output: 2 ... var name1; var name1 = value1; var name1 = value1, name2 = ...
🌐
WsCube Tech
wscubetech.com › resources › javascript › scope
Scope in JavaScript: All Types With Examples
October 8, 2025 - Learn all about JavaScript scope with examples. Understand global, local, block, and function scopes to master coding effectively in JavaScript.
🌐
TutorialsPoint
tutorialspoint.com › javascript › javascript_variable_scope.htm
JavaScript - Variable Scope
Whenever you define the variable using the 'let' or 'const' keyword inside the block, like loop block, if-else block, etc., it can't be accessible outside the block. In JavaScript, each function creates a scope. The variables defined inside a function have function scope.
🌐
DigitalOcean
digitalocean.com › community › tutorials › understanding-variables-scope-hoisting-in-javascript
Understanding Variables, Scope, and Hoisting in JavaScript | DigitalOcean
August 24, 2021 - Scope in JavaScript refers to the current context of code, which determines the accessibility of variables to JavaScript.
🌐
daily.dev
daily.dev › home › blog › get into tech › concepts in javascript: understanding scope
Concepts in JavaScript: Understanding Scope
February 4, 2025 - Scope Types: JavaScript has three main types of scope: Global, Function, and Block scope. Keywords: Variables are declared using var, let, or const, each affecting scope differently.