I know that JavaScript vars point to a value:
var foo = true;
//... later
foo = false;
So in that example I've changed foo
pointing to true
-> foo
pointing to false
, but if I do:
for (var i=0; i<100; i++){
var someVar = i;
}
Am I creating a new var for each iteration?
Is there any difference in the following two ways of doing the same?
var myvar;
for (var i=0; i<100; i++){
myvar = i;
}
and
for (var i=0; i<100; i++){
var myvar = i;
}
If so, why?
Variables in JavaScript (and most other programming languages) are stored in two places: stack and heap. A stack is usually a continuous region of memory allocating local context for each executing function. Heap is a much larger region storing everything allocated dynamically.
A variable is a symbolic name for (or reference to) information. The variable's name represents what information the variable contains. They are called variables because the represented information can change but the operations on the variable remain the same.
Updated on July 03, 2019. The dollar sign ($) and the underscore (_) characters are JavaScript identifiers, which just means that they identify an object in the same way a name would. The objects they identify include things such as variables, functions, properties, events, and objects.
There is no block scope in Javascript ES5 and earlier, only function scope. Furthermore, the declarations of all javascript variables declared within a function scope are automatically "hoisted" to the top of the function.
So, declaring a variable within a loop isn't doing anything different than declaring it at the top of the function and then referencing it within the loop.
See these two references for some useful explanation: http://www.adequatelygood.com/2010/2/JavaScript-Scoping-and-Hoisting and http://net.tutsplus.com/tutorials/javascript-ajax/quick-tip-javascript-hoisting-explained/.
Note: the assignment to a variable is not hoisted, just the declaration of the variable. So, if you do this:
function a() {
for (var i=0; i<100; i++){
var myvar = i;
}
}
It works like this:
function a() {
var myvar;
for (var i=0; i<100; i++){
myvar = i;
}
}
If you wanted to create a new scope inside your for
loop, you could use an IIFE (immediately invoked function expression) like this:
function a() {
for (var i=0; i<100; i++){
(function() {
var myvar = i;
// myvar is now a separate variable for each time through the for loop
})();
}
}
Update in 2015. ES6 (or sometimes called ES2015) offers the let
declaration which does offer block scope. In that case a let
variable declaration is hoisted only to the top of the current block scope. As of mid 2015, this is not yet widely implemented in browsers, but is coming soon and it is available in server-side environments like node.js or via transpilers.
So, in ES6 if you did this:
for (let i=0; i<100; i++){
let someVar = i;
}
Both i
and someVar
would be local to the loop only.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With