The main uses of Let and Var is to declare a variable, based on the usage you would use either "Let" OR "Var ".
Here are some of the differences based on which it becomes easier to identify which one to use.
1. Var was present right from before where as let is present from es6 (ES2015)
Ok, this difference does not make much of a difference as far as using it is considered [:P]
2. Let is block scoped and var is function scope.
This difference would make much effect. Lets dive a bit dipper to understand.
Let is block scoped, which means it is not available outside a pair of parenthesis {}.
eg:
function a() {
{
let localLetVar = 'local let var';
var localVar = 'local variable'
}
console.log(localLetVar) // this will print undefined as it is not available outside the parenthesis.
console.log(localVar) // this will print its value
}
a();
From the above example it should be clear, that "let" is block scoped, whereas "var " is function scoped, which means it is available inside until function ends.
3. For variables declared with "Var", Javascript hoisting happens, whereas variables declared with 'let' does not support javascript hoisting.
We definitely need to understand this, to avoid any surprises later in the code.
Eg:
console.log(a); // this will print undefined as hoisting happens i.e declaration happens for 'a'
var a=1;
console.log(b); // this will print error "b is not defined".
let b=2;
Lets also understand what is it with "const".
We all know "const" as the name suggests is like final in oop, but in javascript there is also something more to it. Lets understand with an example.
In cases of using objects/arrays in javascript.
const a=[1,2];
a.push(3) // allowed
object could be modified, i.e adding or removing values but not reassign.
you cannot do something like again after assigning.
a = [1,2,3]; // this is not allowed
In case of Let/var it allows reassigning as well.
Hopefully the above differences will help you make a good decision. :)
Here are some of the differences based on which it becomes easier to identify which one to use.
1. Var was present right from before where as let is present from es6 (ES2015)
Ok, this difference does not make much of a difference as far as using it is considered [:P]
2. Let is block scoped and var is function scope.
This difference would make much effect. Lets dive a bit dipper to understand.
Let is block scoped, which means it is not available outside a pair of parenthesis {}.
eg:
function a() {
{
let localLetVar = 'local let var';
var localVar = 'local variable'
}
console.log(localLetVar) // this will print undefined as it is not available outside the parenthesis.
console.log(localVar) // this will print its value
}
a();
From the above example it should be clear, that "let" is block scoped, whereas "var " is function scoped, which means it is available inside until function ends.
3. For variables declared with "Var", Javascript hoisting happens, whereas variables declared with 'let' does not support javascript hoisting.
We definitely need to understand this, to avoid any surprises later in the code.
Eg:
console.log(a); // this will print undefined as hoisting happens i.e declaration happens for 'a'
var a=1;
console.log(b); // this will print error "b is not defined".
let b=2;
Lets also understand what is it with "const".
We all know "const" as the name suggests is like final in oop, but in javascript there is also something more to it. Lets understand with an example.
In cases of using objects/arrays in javascript.
const a=[1,2];
a.push(3) // allowed
object could be modified, i.e adding or removing values but not reassign.
you cannot do something like again after assigning.
a = [1,2,3]; // this is not allowed
In case of Let/var it allows reassigning as well.
Hopefully the above differences will help you make a good decision. :)
No comments:
Post a Comment