[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 Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s