In the book I'm reading (JavaScript and JQuery - Interactive Front End Development by Jon Duckett), an interesting error appears or (at least I think so) that does not stop the code from working:
for (var i = [0]; i < options.length; i++) { addEvent(options[i], 'click', radioChanged); }
This is the part of the script that goes through all the switches in the form and attaches an event listener (it doesn't really matter what it does).
But...
Of course, if you replace var i = [0] with var i = 0, the code still works.
When you add some warnings to check the value of i in each iteration of the loop and type i, in the second iteration type I change from an object (after all, in the first iteration it is an array) to a number, This is a kind of implicit type conversion, with which I still have not came across (and google doesn't help much). Can anyone explain what is happening under the hood?
for (var i = [0]; i < options.length; i++) { addEvent(options[i], 'click', radioChanged); alert(i);
javascript type-conversion for-loop
Tadek
source share