Table of contents
No headings in the article.
As a javascript devloper. We all Know about let,const, var , keywords are used to declared variable and assign a value , In this article i am talking about difference between these keywords.
Before Moving to this Topic , we know about term block
Block is nothing but anything inside curly bracket which is "{ }" called block
Basically there are three type of scope
Global scope
Function scope
Local scope
var, let, const works around this three scope, let's understand better with the help of example
- let's see the behavior of var, let, and const keywords in the local scope variable declared inside the scope called local scope .
Lets take example
{
let name = 'fuzz ';
const class = 11;
var age = 23;
}
console.log(name); // Uncaught ReferenceError: name is not defined
console.log(class); // Uncaught ReferenceError: class is not defined
console.log(age); // 23
in the above example , let ,const are giving error and var return value so here we have see var is acess globally , we discuss further about it . so always prefer let and const keyword inside block scope which is local scope . it means let and const is local scope
Lets see how these js variables works inside function scope
- variables inside the function scope using let, var, and const keywords are not accessed outside the function. it gives error
lets take example of function scope
// student() is a function
function student() {
let name = "peter";
const id = 007;
var age = 23;
}
student();
console.log(name); // Uncaught ReferenceError: name is not defined
console.log(id); // Uncaught ReferenceError: id is not defined
console.log(age); // Uncaught ReferenceError: age is not defined
As you saw in the above example, all variables return errors while executing outside the function. not even var keyword . so the variable declared with the var keyword is function scope
- global scope
variable acess outside the block and function we call global scope.
exapmle
let name = "guffy";
const id = 101;
var age = 23;
// student() is a function
function student() {
console.log(name); // guffy
console.log(id); // 101
console.log(age); // 23
}
student ();
console.log(name); // guffy
console.log(id); // 101
console.log(age); // 23
conclusion
Var keywords is function scope
Let is block scope
const is block scope
Here in above Example . all variables acess anypart from the javascript program.
- More difference of let const and var keywords **
in the var keyword we can reassign the value, so it is updated and redeclared
in let case, we can also reassign the value ,so its is updated but not redeclared
in const case, we can never reassign the value, so neither be updated and nor be redeclared
when we try to execute a variable before declaration , using var keyword in this case it gives an undefined error.
when we try to execute the variable before declaration then in the let and const case its gives a reference error.
That's all. It would be really helpful if you give feedback about this article .Thank you