1
Reply

Difference between var and let

Rehman Shahid

Rehman Shahid

Jul 22, 2017
241
0

    difference between var and let lies in their scope i.e var scope lies to the nearest of function block and let scope lies to the nearest of enclosing blockExample:function demoVarAndLet() {// var variable scopefor (var i = 0; i < 5; i++) {}if (typeof i === 'undefined') {console.log('i(var) is undefiend and not accessible after the loop')} else {console.log('i is accessible after for loop i:' + i + ' type: var ');}for (let j = 0; j < 5; j++) {// let variable scope}if (typeof j === 'undefined') {console.log('j(let) is undefiend and not accessible after the loop');} else {console.log('j is accessible after for loop i:' + i + ' type: let')} }For Live Demo: https://jsfiddle.net/rehmanshahid/3Lnyzd49/

    Rehman Shahid
    July 22, 2017
    0