I also like to add the fact that in a for loop, you can access the declared variables with var, when you can't with a let.
"use strict";for(varnumber=0;number<5;number++){// ...}console.log(number);// 5for(letindex=0;index<5;index++){// ...}console.log(index);// ReferenceError: index is not defined
Thank you.
Actually the first difference I wrote explains this. For loops are actually block. So if you declare a variable with "Let" inside the "For" block, you can't access it from outside of the "For" block.
But if you declare with "Var" you can access it because variables declared with "Var" have "function scope".
Indeed, variable are blocked scoped, except for those who are declared with the var keyword and your article explain the thing really well.
But if you look closely, for loops are kind of special, especially since the variable is declared inside the parens of the for loop, and not inside the body (the block).
So this might be kind of confusing for newcomers! Especially those who use the var keyword inside of the for loop. I thought this would be great to have that in your article.
There was a very special talk from the Google YouTube channel about that. It is really cool (and a cool show!)
Nice, neat and clear article!
I also like to add the fact that in a
for
loop, you can access the declared variables withvar
, when you can't with alet
.Simple use-case, but useful to know IMO.
Thank you.
Actually the first difference I wrote explains this. For loops are actually block. So if you declare a variable with "Let" inside the "For" block, you can't access it from outside of the "For" block.
But if you declare with "Var" you can access it because variables declared with "Var" have "function scope".
Indeed, variable are blocked scoped, except for those who are declared with the
var
keyword and your article explain the thing really well.But if you look closely,
for
loops are kind of special, especially since the variable is declared inside the parens of thefor
loop, and not inside the body (the block).So this might be kind of confusing for newcomers! Especially those who use the
var
keyword inside of thefor
loop. I thought this would be great to have that in your article.There was a very special talk from the Google YouTube channel about that. It is really cool (and a cool show!)
Indeed, for loops are... complicated!
Thank you.
I did not watch this video. I will watch it and try to add it.
I watched the video. But I am not too sure whether I should add or not? I'm afraid it will cause confusion for beginners.