Advertisement
benjaminvr

Codecademy - Scope pollution II

Jul 7th, 2021
218
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. // The first function redeclares (let) the air within
  3. // the if-block -- a small example of scope pollution
  4. // by re-using variable names
  5.  
  6. let logMyNightSky = () => {
  7.   const planet = 'Venus';
  8.   let air = 0;
  9.  
  10.   if(planet === 'Venus'){
  11.     let air = 1;
  12.     console.log(`Function 1:\nAir within if-block: ${air}`);
  13.   }
  14.   console.log(`Air outside of if-block: ${air}`)
  15. }
  16.  
  17. // The second function overwrites the air within the
  18. // function by not redeclaring it (no let), as a
  19. // result the more wider scoped variable's value is
  20. // overwritten
  21.  
  22. let logMyGround = () => {
  23.   const soil = 'Clay';
  24.   let air = 0;
  25.  
  26.   if(soil === 'Clay'){
  27.     air = 1;
  28.     console.log(`\nFunction 2:\nAir within if-block: ${air}`)
  29.   }
  30.   console.log(`Air outside of if-block: ${air}`);
  31. }
  32.  
  33. /* Output =
  34. Function 1:
  35. Air within if-block: 1
  36. Air outside of if-block: 0
  37.  
  38. Function 2:
  39. Air within if-block: 1
  40. Air outside of if-block: 1
  41. */
  42. logMyNightSky();
  43. logMyGround();
  44.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement