I’m from python and I’m struggling to get JS basic in terms of scopes and closures.
I’m faced with a tricky moment using forEach loop.
let args = [{},null,"3"]
Case #1
var argumentsLength = function(...args) {
let counter = 0;
args.forEach((_) => {
counter++;
});
return counter;
};
Result: 4
Case #2
let counter = 0;
var argumentsLength = function(...args) {
args.forEach((_) => {
counter++;
});
return counter;
};
Result: 3
In case #2, I had an extra executing of a increment operator. This is because of variable “`counter“`, but what the difference between declarating variables in block and outside the block. How does it work? And what I’ve to read to get this concept.
akcore is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
1