Learn what is the difference between let vs var in javascript.
They are two scopes to which variables are bounded in javascript
1.Function scope:- Variables declared inside functions.
function abcd(){ //I am function scoped var a = 10; }
and
2.Block scope:-
Block or lexical scopes are the boundaries in which declared variables are not accessible outside it. This means the variables declared inside it are available inside given block and its sub-blocks.
Variables declared inside block level statements like for loop, if else, try catch etc.
while(true){ // I am block scoped var a = 10; }
Variables declared inside block scope can be accessed outside and this used to cause bugs, as it was common for a developer to assign different values to any variables. That is why Let and Const, two new ways of declaring variables in javascript are introduced in ES6.
Let us see what is the major difference between var and let in javascript.
We will first explore var
.
var in javascript.
var
are function scoped
which means they are still accessible outside the block scope
even though they are declared inside them.
//for loop is block scoped for (var i = 0; i < 10; i++) { var iAmInside = "I am available outside of the loop"; } console.log(iAmInside); // I am available outside of the loop //block scope if(true){ var inside = "Inside"; } console.log(inside); // Inside //Function scoped function myFunc(){ var functionScoped = "I am available inside this function"; console.log(functionScoped); } myFunc(); // I am available inside this function console.log(functionScoped); // ReferenceError: functionScoped is not defined
Explanation
In the first and second example, the value of the var
leaked out of the block-scope and could be accessed from outside of it, whereas in the third example var was confined inside a function-scope and we could not access it from outside.
This happens because of Hoisting.
var
are treated as if they are at the top of the function (or global scope) regardless of where the actual declaration occurs, this is called hoisting. For a demonstration see the following example.
var inside; // hoisted on the top of the function. As there is no function so it is present in the global scope. //block scope if(true){ var inside = "Inside"; } console.log(inside); //Inside //Function scoped In this case value is hoisted inside the function function getValue(condition) { if (condition) { var value = "blue"; return value; } else { // value exists here with a value of undefined return value; } // value exists here with a value of undefined } console.log(getValue(true)); // blue console.log(getValue(false)); // undefined //While execution it is hoisted like this internally function getValue(condition) { var value; //value is hoisted as there is no value attached, so it is undefined. if (condition) { var value = "blue"; return value; } else { // value exists here with a value of undefined return value; } // value exists here with a value of undefined } console.log(getValue(true)); // blue console.log(getValue(false)); // undefined
We can redeclare var
keyword any number of time we want.
var a = 10; var b = 20; var c = 30; console.log(c); // 30
let in javascript
let
are declared same as var
but it limits the variable scope to the given block. That is why we should declare let
at the top of the block so that is accessible throughout the block and its sub-blocks.
function getValue(condition) { if (condition) { let value = "blue"; // accessible inside the given scope only return value; } else { // value doesnot exits here return value; } // value doesnot exits here } console.log(getValue(true)); // blue console.log(getValue(false)); // ReferenceError: value is not defined let x = 10; if(x == 10){ let x = 11; console.log(x); //11 value of x inside if block } console.log(x); //10 value of x
No redeclaration with let
We cannot redeclare a given variable with the same name again in the given block. Doing so will result is an error.
var c = 0; let c = 0; //SyntaxError: Identifier 'c' has already been declared var c = 10; if(c == 10){ let c = 11; console.log(c); //11 This will work as it is declared in another scope }
The Temporal Dead Zone (TDZ) for let vs var in javascript
According to MDN:
In ECMAScript 2015, let bindings are not subject to Variable Hoisting, which means that let declarations do not move to the top of the current execution context. Referencing the variable in the block before the initialization results in a ReferenceError (contrary to a variable declared with var, which will just have the undefined value). The variable is in a “temporal dead zone” from the start of the block until the initialization is processed.
function do_something() { console.log(bar); // undefined console.log(foo); // ReferenceError: foo is not defined var bar = 1; let foo = 2; } do_something();
Temporal Dead Zone with lexical or block scoping variable declaration
function test(){ var foo = 33; if (true) { let foo = (foo + 55); // ReferenceError: foo is not defined } } test();
Due to lexical or block scoping let foo = (foo + 55)
access the foo
of the current block that is inside the if
condition. It does not access the var foo = 33;
as let
is blocked scope. let foo
is declared but it is not initialized that is why it is still in temporal dead zone.
As there was only one way of declaring variable in javascript before ES6's it is now possible to declare variables with three different ways (var, let and const).
When to use Var vs Let
There is no as such rule stating where to use each of them, Everyone has different opinions.
But according to the properties of these three, it should be used as follows.
- Use
var
for top-level variables that are shared across many (especially larger) scopes. let
can used for localized variables in smaller scopes.