The question about JS

some time ago, I encountered such a question in the interview:


 var num=0;
 for(var i=0;i<10;iPP)
    {
        var num=1;
        num+=i;
    }
 console.log(num);
numnumconsole.lognum09
Nov.19,2021

first, the final answer should be 10;
reason:
1. The repeated declaration of variables will be ignored, but the assignment will not, so each cycle your num starts with 1
2.i < 10


is 10


this loop and var num = 0; in fact, it's in a function, and the for method itself is not a function, is it?. And try to output 10


because the code block is equivalent to a thing at the same level

Menu