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 OverflowAre JavaScript variables declared in the body of an if statement scoped to the body? - Stack Overflow
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.comWhy Javascript devs hate "var" function scoped variable declarations, when python devs are using function scoped variables without any problem?
How do I avoid polluting the global scope in JavaScript
Videos
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.
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.
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
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.
I did not know this! What is emscripten?