javascript - Closure and let -


iam struggling understand why results different in bellow example .

var arr = [];    function build(){  (var = 0; < 3; i++) {    let j = i;    console.log('j = ' + j + ' ' + i);    arr.push(function(){      console.log(j);    });    }        return arr;  }      var newarr = build();  newarr[0]();  newarr[1]();  newarr[2]();

what dont understand , if remove line let j = i; , replace j var , console.log(i); output 3 , 3, 3 because time run newarr0; value 3 . whats difference let j ? isnt same ? time run newarr0; final value of j wouldnt 3 again ?

i know difference between var , let . dont understand in situation how act between each other ?


Comments

Popular posts from this blog

ios - MKAnnotationView layer is not of expected type: MKLayer -

ZeroMQ on Windows, with Qt Creator -

unity3d - Unity SceneManager.LoadScene quits application -