Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // The first function redeclares (let) the air within
- // the if-block -- a small example of scope pollution
- // by re-using variable names
- let logMyNightSky = () => {
- const planet = 'Venus';
- let air = 0;
- if(planet === 'Venus'){
- let air = 1;
- console.log(`Function 1:\nAir within if-block: ${air}`);
- }
- console.log(`Air outside of if-block: ${air}`)
- }
- // The second function overwrites the air within the
- // function by not redeclaring it (no let), as a
- // result the more wider scoped variable's value is
- // overwritten
- let logMyGround = () => {
- const soil = 'Clay';
- let air = 0;
- if(soil === 'Clay'){
- air = 1;
- console.log(`\nFunction 2:\nAir within if-block: ${air}`)
- }
- console.log(`Air outside of if-block: ${air}`);
- }
- /* Output =
- Function 1:
- Air within if-block: 1
- Air outside of if-block: 0
- Function 2:
- Air within if-block: 1
- Air outside of if-block: 1
- */
- logMyNightSky();
- logMyGround();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement