For Loop in Javascript outputs value only from last iteration

前端 未结 5 1796
一向
一向 2020-12-11 07:44

I have this Javascript code, which works as expected:

5条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-11 08:50

    The variable i will always refer to the current value of i (not the value of i when called) in this situation.

    That's how scope works in JavaScript.

    See the answer by @Esailija for a solution.

提交回复
热议问题