Hey smart people!I have a doubt regarding 2nd lessonās content in ES6 module of JAVASCRIPT. Can you tell the the difference between the below two programs.
var numArray = [];
for (var i = 0; i < 3; i++) {
numArray.push(i);
}
console.log(numArray);
// returns [0, 1, 2]
console.log(i);
// returns 3
In the first code,var inside the "for" loop right so i thought it was a local variable so it wouldn't return the value of i out of the "for" block.
In the first code,var inside the āforā loop right so i thought it was a local variable so it wouldnāt return the value of i out of the āforā block.
var numArray = [];
var i;
for (i = 0; i < 3; i++) {
numArray.push(i);
}
console.log(numArray);
// returns [0, 1, 2]
console.log(i);
// returns 3
```But over here var is declared globally and the result is same as i expected .
How is the result same when one var is declared locally and another globally.
I am confused with the first program.
Thanks in advance for taking your valuable time and answering.
HI sir!I have a doubt regarding 2nd lessonās content in ES6 module of JAVASCRIPT. Can you tell the the difference between the below two programs in terms of console.log(i).
var numArray = [];
for (var i = 0; i < 3; i++) {
numArray.push(i);
}
console.log(numArray);
// returns [0, 1, 2]
console.log(i);
// returns 3
In the first code,var inside the "for" loop right so i thought it was a local variable so it wouldn't return the value of i out of the "for" block. console.log(i) should return an error RIGHT?
In the first code,var i inside the āforā loop right so i thought it was a local variable so it wouldnāt return the value of i out of the āforā block.
var numArray = [];
var i;
for (i = 0; i < 3; i++) {
numArray.push(i);
}
console.log(numArray);
// returns [0, 1, 2]
console.log(i);
// returns 3
```But over here var i is declared globally and the result is same as i expected .
How is the result same when one var is declared locally and another globally.
I am confused with the first program.
Thanks in advance for taking your valuable time and answering.
Hey! sorry if i didnt mention i was talking about console.log(i);According to me the console.log(i) should return an error in the first program since there it is a local variable.