Difference between var and let variable declaration is the “scope”

Main difference between var and let variable declaration is the “scope”.
var is scoped to the nearest function block
let is scoped to the nearest enclosing block, this scope can be smaller than a function block

Both types will have global scope if defined outside a function block, no difference work exactly same way

Scope: {Global} Example

let letValue = 'Value for let variable'; //globally scoped
var varValve = 'Value for var variable'; //globally scoped
function FirstFunction()
{
//letValue & varValve are visible here
}
function secondFunction()
{
//letValue & varValve are visible here
}
//letValue & varValve are visible here

Also, just like var, variables declared with let are visible before they are declared in their enclosing block.
Scope: {function} Example
They are identical when used like this in a function block.


function getValueFunction() {
let letValue = 'Value for let variable'; //function block scoped
var varValve = 'Value for var variable'; //function block scoped

//letValue & varValve are visible here

};//--> nearest enclosing block, let scope ends here**

//letValue & varValve are NOT visible here

Scope: {Block} Example
Here is the difference. let is only visible in the for() loop and var is visible to the whole function.


<code>function testMethod1() {
//letValue is NOT visible out here</code>

for( let letValue = 0; letValue < 5; letValue++ ) {

//letValue is only visible in here (and in the for() parentheses)

};//--> nearest enclosing block, let scope ends here**

//letValue is NOT visible out here

};

var is visible in the scope,  that is the nearest function block

function testMethod2() {
//varValve is visible out here</code></pre>

for( var varValve = 0; varValve < 5; varValve++ ) {
//varValve is visible to the whole function
};

//varValve is visible and is scoped to the nearest function block

};//-->nearest function block ends

And let can also be used to create its own enclosing block.


function testMethod3() {
//letValue is NOT visible out here</code>

let( letValue = 'now' ) {

//letValue is only visible in here

};//-->nearest enclosing block ends

//letValue is *not* visible out here
};

Hence, let works very much like var. The main difference is that the scope of a var variable is the entire enclosing function. ECMAScript is the standard and let is included in the 6th edition draft and will most likely be in the final specification. IE11 added support for let msdn.microsoft.com/en-us/library/ie/dn342892%28v=vs.85%29.aspx

Advertisements
This entry was posted in javascript and tagged , . Bookmark the permalink.

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