Is there a performance difference between 'let' and 'var' in JavaScript

前端 未结 5 1358
慢半拍i
慢半拍i 2020-11-28 05:23

The difference between these two keywords in terms of scoping has already been thoroughly discussed here, but I was wondering if there is any kind of performance difference

5条回答
  •  遥遥无期
    2020-11-28 05:32

    var: Declare a variable, value initialization optional. Let is faster in outside scope.

    let: Declare a local variable with block scope. Let is a little bit slow in inside loop.

    Ex:

    var a;
    a = 1;
    a = 2; //re-intilize possibe
    var a = 3; //re-declare
    console.log(a); //3
    
    let b;
    b = 5;
    b = 6; //re-intilize possibe
    // let b = 7; //re-declare not possible
    console.log(b);
    

提交回复
热议问题