[JS] What is the difference between “var” and “let” to declare a variable?

If you declare a variable via “var” syntax it can be accessed outside of the method as opposed to declaring a variable using “let”syntax.

For understand better look the example below :

        for(let i=0; i<3; i++) {
            {...} 
        }
        console.log(i) // undefined
        for(var i=0; i<3; i++) {
            {...} 
        }
        console.log(i) // 2

 

Leave a comment