• "for...in" statement question.

Question related to mission The Most Wanted Letter

 

I got the following error: "ReferenceError: letter is not defined, at getMostFrequentLetter (evalmachine.<anonymous>:24:10), at ClientLoop.mostWanted (evalmachine.<anonymous>:9:12)"

in the following code: for (letter in lettersOccurence) { //some code }

I can fix this error by addiing: let letter = ""; before my for loop, or just by typing "(for let letter in lettersOccurence)".

My question is why most browsers will accept the following codes: 1. for (element in sth) 2. for (i=0; i<10; i++)

Instead of throwing an error and forcing me to write sth like this: 1. for (var element in sth) 2. for (var i=0; i<10; i++)

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for...in

In documentation the following example is shown: (for var i=0....), var/let keyword before i. I'm not 100% sure but in most languages I'd get an error if I won't declare i in my for loop. What are your thoughts about that? I'm asking this question because I'm solving tasks in my local editor. When everything is OK, I copy it to checkio and sometimes I get an error and I'm confused. And this code "for (letter in lettersOccurence)" would work even at developer.mozilla.org local code interpreter, so why it doesn't work here?