please see the code below
myduplicates = iterationProc(2, [1, 2, 3, 1]);
console.log(myduplicates);
function iterationProc(curItem, currArr) {
let duplicates = -1; // line A
currArr.forEach((item, index) => {
if (curItem === item) {
duplicates += 1; // line B
}
});
return duplicates; // line C
}
I am having some difficulty understanding scoping concepts. I have commented three lines A,B, and C. At line B the variable duplicates is getting the value from line A due to lexical scoping in javascript. I understand this. But in my understanding since line C is an outer block as compared to line B so variable duplicates at line C should not get value from line B but retain the value from line A. However at line C the variable does get the value from variable at line B. What am i missing ?