Do local variables inside of a loop get garbage collected?

后端 未结 3 1339
Happy的楠姐
Happy的楠姐 2021-01-27 03:17

I\'m wondering if it is more efficient to place any vars referenced within a loop, outside of the loop - or can they get garbage collected like vars inside of a function?

<
3条回答
  •  误落风尘
    2021-01-27 04:03

    var is function scoped, not blocked scoped, so it does not matter whether they appear inside the loop or not. What is the scope of variables in JavaScript? explains this distinction.

    The next version of JavaScript will have let-scoped variables and the value stored in those would become collectible at the end of a loop body run if declared inside the loop.

提交回复
热议问题