javascript - Let Scope output -


can explain why first console in test function gives value 99.

let l = 99;  function test(){     console.log(l);     if(true){         let l = 7;         console.log(l);     } }  test(); console.log(l); 

added comments explanation

// global scope  let l = 99;    function test(){      // console.log(r); can't accessed here      // can see global scope      console.log(l);      if(true){          // limited scope          let l = 7, r=0;          // l overides global scope           console.log(l);      }      // console.log(r); can't accessed here  }    test();  // can see l in global scope  console.log(l);

to learn scope of let please refer mdn blog


Comments

Popular posts from this blog

ubuntu - PHP script to find files of certain extensions in a directory, returns populated array when run in browser, but empty array when run from terminal -

php - How can i create a user dashboard -

javascript - How to detect toggling of the fullscreen-toolbar in jQuery Mobile? -