I have the following code:
$(document).ready(function() { ... function funcA(items) { // Ранее этого места index нигде не фигурирует. for (index = 0; index < items.length; index++) { var a = funcB(items[index]); ... } } } function funcB(id) { for (index = 0; index < initJson.length; index++) { item = initJson[index]; if (item.id == id) { return item; } } return -1; } In the funcA function and the funcB function funcB I use variables in the loop with the same name ( index ), which at first glance are completely unrelated to each other, since they must exist in different “visibility spaces”. Using the debugger, I see that the value of the index variable in the funcA function, after calling funcB becomes equal to the last value in the funcB function. Tell me why I see this behavior? Behavior does not occur if, in a loop, I declare a variable as var index = 0 , yet it is not clear how and why the variable from the scope of the calling code falls and changes in the callee.