Javascript - scoping an incrementing-loop counter only to that loop
I've run into this error multiple times now, and wondering if there's a way to have the code auto control for it?
var length = some_array.length
for (var i = 0; i < length; i ++) {
someMethod(some_array[i].name)
}
function someMethod(name) {
var length = other_array.length
for (var i = 0; i < length; i ++ ) {
// some code
}
}
The code above doesn't run properly because the inner i
and length
carry over to the outer level code. So to actually make it work, I have to manually set different variables to manage the scoping. I.e., something like the below works:
var length = some_array.length
for (var i = 0; i < length; i ++) {
someMethod(some_array[i].name)
}
function someMethod(name) {
var s_length = other_array.s_length
for (var s_i = 0; s_i < length; s_i ++ ) {
// some code
}
}
But of course, it's hard to always remember when an inner function has another loop in it. Wondering if there's a way to control it so that loop variables are always only constrained to THAT loop itself.
javascript
add a comment |
I've run into this error multiple times now, and wondering if there's a way to have the code auto control for it?
var length = some_array.length
for (var i = 0; i < length; i ++) {
someMethod(some_array[i].name)
}
function someMethod(name) {
var length = other_array.length
for (var i = 0; i < length; i ++ ) {
// some code
}
}
The code above doesn't run properly because the inner i
and length
carry over to the outer level code. So to actually make it work, I have to manually set different variables to manage the scoping. I.e., something like the below works:
var length = some_array.length
for (var i = 0; i < length; i ++) {
someMethod(some_array[i].name)
}
function someMethod(name) {
var s_length = other_array.s_length
for (var s_i = 0; s_i < length; s_i ++ ) {
// some code
}
}
But of course, it's hard to always remember when an inner function has another loop in it. Wondering if there's a way to control it so that loop variables are always only constrained to THAT loop itself.
javascript
3
Take a look at thelet
keyword.
– ggorlen
Nov 26 '18 at 2:21
2
The code above doesn't run properly because the inner i and length carry over to the outer level
no, they don't - you must be doing something else wrong
– Jaromanda X
Nov 26 '18 at 2:40
add a comment |
I've run into this error multiple times now, and wondering if there's a way to have the code auto control for it?
var length = some_array.length
for (var i = 0; i < length; i ++) {
someMethod(some_array[i].name)
}
function someMethod(name) {
var length = other_array.length
for (var i = 0; i < length; i ++ ) {
// some code
}
}
The code above doesn't run properly because the inner i
and length
carry over to the outer level code. So to actually make it work, I have to manually set different variables to manage the scoping. I.e., something like the below works:
var length = some_array.length
for (var i = 0; i < length; i ++) {
someMethod(some_array[i].name)
}
function someMethod(name) {
var s_length = other_array.s_length
for (var s_i = 0; s_i < length; s_i ++ ) {
// some code
}
}
But of course, it's hard to always remember when an inner function has another loop in it. Wondering if there's a way to control it so that loop variables are always only constrained to THAT loop itself.
javascript
I've run into this error multiple times now, and wondering if there's a way to have the code auto control for it?
var length = some_array.length
for (var i = 0; i < length; i ++) {
someMethod(some_array[i].name)
}
function someMethod(name) {
var length = other_array.length
for (var i = 0; i < length; i ++ ) {
// some code
}
}
The code above doesn't run properly because the inner i
and length
carry over to the outer level code. So to actually make it work, I have to manually set different variables to manage the scoping. I.e., something like the below works:
var length = some_array.length
for (var i = 0; i < length; i ++) {
someMethod(some_array[i].name)
}
function someMethod(name) {
var s_length = other_array.s_length
for (var s_i = 0; s_i < length; s_i ++ ) {
// some code
}
}
But of course, it's hard to always remember when an inner function has another loop in it. Wondering if there's a way to control it so that loop variables are always only constrained to THAT loop itself.
javascript
javascript
edited Nov 26 '18 at 2:21
ggorlen
7,6483926
7,6483926
asked Nov 26 '18 at 2:19
jamesjames
1,72732859
1,72732859
3
Take a look at thelet
keyword.
– ggorlen
Nov 26 '18 at 2:21
2
The code above doesn't run properly because the inner i and length carry over to the outer level
no, they don't - you must be doing something else wrong
– Jaromanda X
Nov 26 '18 at 2:40
add a comment |
3
Take a look at thelet
keyword.
– ggorlen
Nov 26 '18 at 2:21
2
The code above doesn't run properly because the inner i and length carry over to the outer level
no, they don't - you must be doing something else wrong
– Jaromanda X
Nov 26 '18 at 2:40
3
3
Take a look at the
let
keyword.– ggorlen
Nov 26 '18 at 2:21
Take a look at the
let
keyword.– ggorlen
Nov 26 '18 at 2:21
2
2
The code above doesn't run properly because the inner i and length carry over to the outer level
no, they don't - you must be doing something else wrong– Jaromanda X
Nov 26 '18 at 2:40
The code above doesn't run properly because the inner i and length carry over to the outer level
no, they don't - you must be doing something else wrong– Jaromanda X
Nov 26 '18 at 2:40
add a comment |
2 Answers
2
active
oldest
votes
This is why let
keyword is a better option than var- it keeps the scope to just the function or method that calls it.
The following snippet consoles -
0 / test 1 / 0 / test a
0 / test 1 / 1 / test b
1 / test 2 / 0 / test a
1 / test 2 / 1 / test b
ie: for each item of some_array - it calls someMethod and consoles the name of the first array as well as each iteration of the second array (other_array).
let some_array=[{id: '1', name: 'test 1'},{id: '2', name: 'test 2'}];
let other_array=[{id: 'a', name: 'test a'},{id: 'b', name: 'test b'}];
let length = some_array.length;
for (let i = 0; i < length; i ++) {
someMethod(i, some_array[i].name)
}
function someMethod(index, name) {
let length = other_array.length
for (let i = 0; i < length; i ++ ) {
console.log(index + ' / ' + name + ' / ' + i + ' / ' + other_array[i].name);
}
}
1
I get the same output regardless oflet
orvar
here. Maybe there's a better example?i
insidesomeMethod
is scoped to the function.
– Mark Meyer
Nov 26 '18 at 2:32
Each i IS scoped to its location - I just amended it to log the i's as well and as you can see - each log shows its place in the sequence - 0/0 , 0,1 , 1,0 ,11 - or in other words - for each of the i's in the first gtopu its calling the method and in the method - its iteratnig over the second list.
– gavgrif
Nov 26 '18 at 2:40
So, maybe I'm just missing the point. What would be different if you replaced all thelet
s withvar
s?
– Mark Meyer
Nov 26 '18 at 2:48
add a comment |
SCOPE
In JavaScript there are two types of scope:
Local scope
Global scope
JavaScript has function scope: Each function creates a new scope.
let
allows you to declare variables that are limited in scope to the block.
Notice how var
create a property on the global object but let
is undefined.
var x = 'global';
let y = 'global';
console.log(this.x); // "global"
console.log(this.y); // undefined
Let's try it with your code.
let some_array = [{ name: 'Angel' }, { name: 'James' }]
let other_array = ['0','1'];
//let i is declared local
for (let i = 0; i < some_array.length; i++) {
someMethod(some_array[i].name) //"let" some_array variable is required here to the local "for loop" scope.
}
function someMethod(name) {
//let i is declared local here again so it's considered a new variable i.
for (let i = 0; i < some_array.length; i++) {
console.log(name)
}
}
For more examples about scope in Javascript
Wouldn'tvar i
also be local tosomeMethod()
?
– Mark Meyer
Nov 26 '18 at 2:50
@MarkMeyerlet
gives you the privilege to declare variables that are limited in scope to the block, statement of expression unlikevar
.var
is rather a keyword which defines a variable globally regardless of block scope
– Angel Roma
Nov 26 '18 at 2:55
That's not quite correct. Inside a functionvar
is scoped to that function.
– Mark Meyer
Nov 26 '18 at 3:01
var
keyword defines a variable globally regardless of block scope.
– Angel Roma
Nov 26 '18 at 3:06
But it doesn't define it globally when used inside a function. In the example you have, it makes no difference whether you uselet
orvar
fori
because the second one is defined inside the functionsomeMethod
. It will be a distinct variable unrelated to thei
outside the function. Nothing changes if you usevar
instead oflet
in your example.
– Mark Meyer
Nov 26 '18 at 3:11
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53473992%2fjavascript-scoping-an-incrementing-loop-counter-only-to-that-loop%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
This is why let
keyword is a better option than var- it keeps the scope to just the function or method that calls it.
The following snippet consoles -
0 / test 1 / 0 / test a
0 / test 1 / 1 / test b
1 / test 2 / 0 / test a
1 / test 2 / 1 / test b
ie: for each item of some_array - it calls someMethod and consoles the name of the first array as well as each iteration of the second array (other_array).
let some_array=[{id: '1', name: 'test 1'},{id: '2', name: 'test 2'}];
let other_array=[{id: 'a', name: 'test a'},{id: 'b', name: 'test b'}];
let length = some_array.length;
for (let i = 0; i < length; i ++) {
someMethod(i, some_array[i].name)
}
function someMethod(index, name) {
let length = other_array.length
for (let i = 0; i < length; i ++ ) {
console.log(index + ' / ' + name + ' / ' + i + ' / ' + other_array[i].name);
}
}
1
I get the same output regardless oflet
orvar
here. Maybe there's a better example?i
insidesomeMethod
is scoped to the function.
– Mark Meyer
Nov 26 '18 at 2:32
Each i IS scoped to its location - I just amended it to log the i's as well and as you can see - each log shows its place in the sequence - 0/0 , 0,1 , 1,0 ,11 - or in other words - for each of the i's in the first gtopu its calling the method and in the method - its iteratnig over the second list.
– gavgrif
Nov 26 '18 at 2:40
So, maybe I'm just missing the point. What would be different if you replaced all thelet
s withvar
s?
– Mark Meyer
Nov 26 '18 at 2:48
add a comment |
This is why let
keyword is a better option than var- it keeps the scope to just the function or method that calls it.
The following snippet consoles -
0 / test 1 / 0 / test a
0 / test 1 / 1 / test b
1 / test 2 / 0 / test a
1 / test 2 / 1 / test b
ie: for each item of some_array - it calls someMethod and consoles the name of the first array as well as each iteration of the second array (other_array).
let some_array=[{id: '1', name: 'test 1'},{id: '2', name: 'test 2'}];
let other_array=[{id: 'a', name: 'test a'},{id: 'b', name: 'test b'}];
let length = some_array.length;
for (let i = 0; i < length; i ++) {
someMethod(i, some_array[i].name)
}
function someMethod(index, name) {
let length = other_array.length
for (let i = 0; i < length; i ++ ) {
console.log(index + ' / ' + name + ' / ' + i + ' / ' + other_array[i].name);
}
}
1
I get the same output regardless oflet
orvar
here. Maybe there's a better example?i
insidesomeMethod
is scoped to the function.
– Mark Meyer
Nov 26 '18 at 2:32
Each i IS scoped to its location - I just amended it to log the i's as well and as you can see - each log shows its place in the sequence - 0/0 , 0,1 , 1,0 ,11 - or in other words - for each of the i's in the first gtopu its calling the method and in the method - its iteratnig over the second list.
– gavgrif
Nov 26 '18 at 2:40
So, maybe I'm just missing the point. What would be different if you replaced all thelet
s withvar
s?
– Mark Meyer
Nov 26 '18 at 2:48
add a comment |
This is why let
keyword is a better option than var- it keeps the scope to just the function or method that calls it.
The following snippet consoles -
0 / test 1 / 0 / test a
0 / test 1 / 1 / test b
1 / test 2 / 0 / test a
1 / test 2 / 1 / test b
ie: for each item of some_array - it calls someMethod and consoles the name of the first array as well as each iteration of the second array (other_array).
let some_array=[{id: '1', name: 'test 1'},{id: '2', name: 'test 2'}];
let other_array=[{id: 'a', name: 'test a'},{id: 'b', name: 'test b'}];
let length = some_array.length;
for (let i = 0; i < length; i ++) {
someMethod(i, some_array[i].name)
}
function someMethod(index, name) {
let length = other_array.length
for (let i = 0; i < length; i ++ ) {
console.log(index + ' / ' + name + ' / ' + i + ' / ' + other_array[i].name);
}
}
This is why let
keyword is a better option than var- it keeps the scope to just the function or method that calls it.
The following snippet consoles -
0 / test 1 / 0 / test a
0 / test 1 / 1 / test b
1 / test 2 / 0 / test a
1 / test 2 / 1 / test b
ie: for each item of some_array - it calls someMethod and consoles the name of the first array as well as each iteration of the second array (other_array).
let some_array=[{id: '1', name: 'test 1'},{id: '2', name: 'test 2'}];
let other_array=[{id: 'a', name: 'test a'},{id: 'b', name: 'test b'}];
let length = some_array.length;
for (let i = 0; i < length; i ++) {
someMethod(i, some_array[i].name)
}
function someMethod(index, name) {
let length = other_array.length
for (let i = 0; i < length; i ++ ) {
console.log(index + ' / ' + name + ' / ' + i + ' / ' + other_array[i].name);
}
}
let some_array=[{id: '1', name: 'test 1'},{id: '2', name: 'test 2'}];
let other_array=[{id: 'a', name: 'test a'},{id: 'b', name: 'test b'}];
let length = some_array.length;
for (let i = 0; i < length; i ++) {
someMethod(i, some_array[i].name)
}
function someMethod(index, name) {
let length = other_array.length
for (let i = 0; i < length; i ++ ) {
console.log(index + ' / ' + name + ' / ' + i + ' / ' + other_array[i].name);
}
}
let some_array=[{id: '1', name: 'test 1'},{id: '2', name: 'test 2'}];
let other_array=[{id: 'a', name: 'test a'},{id: 'b', name: 'test b'}];
let length = some_array.length;
for (let i = 0; i < length; i ++) {
someMethod(i, some_array[i].name)
}
function someMethod(index, name) {
let length = other_array.length
for (let i = 0; i < length; i ++ ) {
console.log(index + ' / ' + name + ' / ' + i + ' / ' + other_array[i].name);
}
}
edited Nov 26 '18 at 2:39
answered Nov 26 '18 at 2:24
gavgrifgavgrif
8,6912918
8,6912918
1
I get the same output regardless oflet
orvar
here. Maybe there's a better example?i
insidesomeMethod
is scoped to the function.
– Mark Meyer
Nov 26 '18 at 2:32
Each i IS scoped to its location - I just amended it to log the i's as well and as you can see - each log shows its place in the sequence - 0/0 , 0,1 , 1,0 ,11 - or in other words - for each of the i's in the first gtopu its calling the method and in the method - its iteratnig over the second list.
– gavgrif
Nov 26 '18 at 2:40
So, maybe I'm just missing the point. What would be different if you replaced all thelet
s withvar
s?
– Mark Meyer
Nov 26 '18 at 2:48
add a comment |
1
I get the same output regardless oflet
orvar
here. Maybe there's a better example?i
insidesomeMethod
is scoped to the function.
– Mark Meyer
Nov 26 '18 at 2:32
Each i IS scoped to its location - I just amended it to log the i's as well and as you can see - each log shows its place in the sequence - 0/0 , 0,1 , 1,0 ,11 - or in other words - for each of the i's in the first gtopu its calling the method and in the method - its iteratnig over the second list.
– gavgrif
Nov 26 '18 at 2:40
So, maybe I'm just missing the point. What would be different if you replaced all thelet
s withvar
s?
– Mark Meyer
Nov 26 '18 at 2:48
1
1
I get the same output regardless of
let
or var
here. Maybe there's a better example? i
inside someMethod
is scoped to the function.– Mark Meyer
Nov 26 '18 at 2:32
I get the same output regardless of
let
or var
here. Maybe there's a better example? i
inside someMethod
is scoped to the function.– Mark Meyer
Nov 26 '18 at 2:32
Each i IS scoped to its location - I just amended it to log the i's as well and as you can see - each log shows its place in the sequence - 0/0 , 0,1 , 1,0 ,11 - or in other words - for each of the i's in the first gtopu its calling the method and in the method - its iteratnig over the second list.
– gavgrif
Nov 26 '18 at 2:40
Each i IS scoped to its location - I just amended it to log the i's as well and as you can see - each log shows its place in the sequence - 0/0 , 0,1 , 1,0 ,11 - or in other words - for each of the i's in the first gtopu its calling the method and in the method - its iteratnig over the second list.
– gavgrif
Nov 26 '18 at 2:40
So, maybe I'm just missing the point. What would be different if you replaced all the
let
s with var
s?– Mark Meyer
Nov 26 '18 at 2:48
So, maybe I'm just missing the point. What would be different if you replaced all the
let
s with var
s?– Mark Meyer
Nov 26 '18 at 2:48
add a comment |
SCOPE
In JavaScript there are two types of scope:
Local scope
Global scope
JavaScript has function scope: Each function creates a new scope.
let
allows you to declare variables that are limited in scope to the block.
Notice how var
create a property on the global object but let
is undefined.
var x = 'global';
let y = 'global';
console.log(this.x); // "global"
console.log(this.y); // undefined
Let's try it with your code.
let some_array = [{ name: 'Angel' }, { name: 'James' }]
let other_array = ['0','1'];
//let i is declared local
for (let i = 0; i < some_array.length; i++) {
someMethod(some_array[i].name) //"let" some_array variable is required here to the local "for loop" scope.
}
function someMethod(name) {
//let i is declared local here again so it's considered a new variable i.
for (let i = 0; i < some_array.length; i++) {
console.log(name)
}
}
For more examples about scope in Javascript
Wouldn'tvar i
also be local tosomeMethod()
?
– Mark Meyer
Nov 26 '18 at 2:50
@MarkMeyerlet
gives you the privilege to declare variables that are limited in scope to the block, statement of expression unlikevar
.var
is rather a keyword which defines a variable globally regardless of block scope
– Angel Roma
Nov 26 '18 at 2:55
That's not quite correct. Inside a functionvar
is scoped to that function.
– Mark Meyer
Nov 26 '18 at 3:01
var
keyword defines a variable globally regardless of block scope.
– Angel Roma
Nov 26 '18 at 3:06
But it doesn't define it globally when used inside a function. In the example you have, it makes no difference whether you uselet
orvar
fori
because the second one is defined inside the functionsomeMethod
. It will be a distinct variable unrelated to thei
outside the function. Nothing changes if you usevar
instead oflet
in your example.
– Mark Meyer
Nov 26 '18 at 3:11
add a comment |
SCOPE
In JavaScript there are two types of scope:
Local scope
Global scope
JavaScript has function scope: Each function creates a new scope.
let
allows you to declare variables that are limited in scope to the block.
Notice how var
create a property on the global object but let
is undefined.
var x = 'global';
let y = 'global';
console.log(this.x); // "global"
console.log(this.y); // undefined
Let's try it with your code.
let some_array = [{ name: 'Angel' }, { name: 'James' }]
let other_array = ['0','1'];
//let i is declared local
for (let i = 0; i < some_array.length; i++) {
someMethod(some_array[i].name) //"let" some_array variable is required here to the local "for loop" scope.
}
function someMethod(name) {
//let i is declared local here again so it's considered a new variable i.
for (let i = 0; i < some_array.length; i++) {
console.log(name)
}
}
For more examples about scope in Javascript
Wouldn'tvar i
also be local tosomeMethod()
?
– Mark Meyer
Nov 26 '18 at 2:50
@MarkMeyerlet
gives you the privilege to declare variables that are limited in scope to the block, statement of expression unlikevar
.var
is rather a keyword which defines a variable globally regardless of block scope
– Angel Roma
Nov 26 '18 at 2:55
That's not quite correct. Inside a functionvar
is scoped to that function.
– Mark Meyer
Nov 26 '18 at 3:01
var
keyword defines a variable globally regardless of block scope.
– Angel Roma
Nov 26 '18 at 3:06
But it doesn't define it globally when used inside a function. In the example you have, it makes no difference whether you uselet
orvar
fori
because the second one is defined inside the functionsomeMethod
. It will be a distinct variable unrelated to thei
outside the function. Nothing changes if you usevar
instead oflet
in your example.
– Mark Meyer
Nov 26 '18 at 3:11
add a comment |
SCOPE
In JavaScript there are two types of scope:
Local scope
Global scope
JavaScript has function scope: Each function creates a new scope.
let
allows you to declare variables that are limited in scope to the block.
Notice how var
create a property on the global object but let
is undefined.
var x = 'global';
let y = 'global';
console.log(this.x); // "global"
console.log(this.y); // undefined
Let's try it with your code.
let some_array = [{ name: 'Angel' }, { name: 'James' }]
let other_array = ['0','1'];
//let i is declared local
for (let i = 0; i < some_array.length; i++) {
someMethod(some_array[i].name) //"let" some_array variable is required here to the local "for loop" scope.
}
function someMethod(name) {
//let i is declared local here again so it's considered a new variable i.
for (let i = 0; i < some_array.length; i++) {
console.log(name)
}
}
For more examples about scope in Javascript
SCOPE
In JavaScript there are two types of scope:
Local scope
Global scope
JavaScript has function scope: Each function creates a new scope.
let
allows you to declare variables that are limited in scope to the block.
Notice how var
create a property on the global object but let
is undefined.
var x = 'global';
let y = 'global';
console.log(this.x); // "global"
console.log(this.y); // undefined
Let's try it with your code.
let some_array = [{ name: 'Angel' }, { name: 'James' }]
let other_array = ['0','1'];
//let i is declared local
for (let i = 0; i < some_array.length; i++) {
someMethod(some_array[i].name) //"let" some_array variable is required here to the local "for loop" scope.
}
function someMethod(name) {
//let i is declared local here again so it's considered a new variable i.
for (let i = 0; i < some_array.length; i++) {
console.log(name)
}
}
For more examples about scope in Javascript
answered Nov 26 '18 at 2:42
Angel RomaAngel Roma
19829
19829
Wouldn'tvar i
also be local tosomeMethod()
?
– Mark Meyer
Nov 26 '18 at 2:50
@MarkMeyerlet
gives you the privilege to declare variables that are limited in scope to the block, statement of expression unlikevar
.var
is rather a keyword which defines a variable globally regardless of block scope
– Angel Roma
Nov 26 '18 at 2:55
That's not quite correct. Inside a functionvar
is scoped to that function.
– Mark Meyer
Nov 26 '18 at 3:01
var
keyword defines a variable globally regardless of block scope.
– Angel Roma
Nov 26 '18 at 3:06
But it doesn't define it globally when used inside a function. In the example you have, it makes no difference whether you uselet
orvar
fori
because the second one is defined inside the functionsomeMethod
. It will be a distinct variable unrelated to thei
outside the function. Nothing changes if you usevar
instead oflet
in your example.
– Mark Meyer
Nov 26 '18 at 3:11
add a comment |
Wouldn'tvar i
also be local tosomeMethod()
?
– Mark Meyer
Nov 26 '18 at 2:50
@MarkMeyerlet
gives you the privilege to declare variables that are limited in scope to the block, statement of expression unlikevar
.var
is rather a keyword which defines a variable globally regardless of block scope
– Angel Roma
Nov 26 '18 at 2:55
That's not quite correct. Inside a functionvar
is scoped to that function.
– Mark Meyer
Nov 26 '18 at 3:01
var
keyword defines a variable globally regardless of block scope.
– Angel Roma
Nov 26 '18 at 3:06
But it doesn't define it globally when used inside a function. In the example you have, it makes no difference whether you uselet
orvar
fori
because the second one is defined inside the functionsomeMethod
. It will be a distinct variable unrelated to thei
outside the function. Nothing changes if you usevar
instead oflet
in your example.
– Mark Meyer
Nov 26 '18 at 3:11
Wouldn't
var i
also be local to someMethod()
?– Mark Meyer
Nov 26 '18 at 2:50
Wouldn't
var i
also be local to someMethod()
?– Mark Meyer
Nov 26 '18 at 2:50
@MarkMeyer
let
gives you the privilege to declare variables that are limited in scope to the block, statement of expression unlike var
. var
is rather a keyword which defines a variable globally regardless of block scope– Angel Roma
Nov 26 '18 at 2:55
@MarkMeyer
let
gives you the privilege to declare variables that are limited in scope to the block, statement of expression unlike var
. var
is rather a keyword which defines a variable globally regardless of block scope– Angel Roma
Nov 26 '18 at 2:55
That's not quite correct. Inside a function
var
is scoped to that function.– Mark Meyer
Nov 26 '18 at 3:01
That's not quite correct. Inside a function
var
is scoped to that function.– Mark Meyer
Nov 26 '18 at 3:01
var
keyword defines a variable globally regardless of block scope.– Angel Roma
Nov 26 '18 at 3:06
var
keyword defines a variable globally regardless of block scope.– Angel Roma
Nov 26 '18 at 3:06
But it doesn't define it globally when used inside a function. In the example you have, it makes no difference whether you use
let
or var
for i
because the second one is defined inside the function someMethod
. It will be a distinct variable unrelated to the i
outside the function. Nothing changes if you use var
instead of let
in your example.– Mark Meyer
Nov 26 '18 at 3:11
But it doesn't define it globally when used inside a function. In the example you have, it makes no difference whether you use
let
or var
for i
because the second one is defined inside the function someMethod
. It will be a distinct variable unrelated to the i
outside the function. Nothing changes if you use var
instead of let
in your example.– Mark Meyer
Nov 26 '18 at 3:11
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53473992%2fjavascript-scoping-an-incrementing-loop-counter-only-to-that-loop%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
3
Take a look at the
let
keyword.– ggorlen
Nov 26 '18 at 2:21
2
The code above doesn't run properly because the inner i and length carry over to the outer level
no, they don't - you must be doing something else wrong– Jaromanda X
Nov 26 '18 at 2:40