What is difference between var let and const in javascript?
A lot of shiny new features came out with ES2015 (ES6). And now, since it's 2020, it's assumed that a lot of JavaScript developers have become familiar with and have started using these features. Show
While this assumption might be partially true, it's still possible that some of these features remain a mystery to some devs. One of the features that came with ES6 is the addition of In this article, we'll discuss Before the advent of ES6, Scope of varScope essentially means where these variables are available for use. The scope is global when a
To understand further, look at the example below.
Here,
We'll get an error which is as a result of var variables can be re-declared and updatedThis means that we can do this within the same scope and won't get an error.
and this also
Hoisting of varHoisting is a JavaScript mechanism where variables and function declarations are moved to the top of their scope before code execution. This means that if we do this:
it is interpreted as this:
So Problem with varThere's a weakness that comes with
So, since If you have used Let
let is block scopedA block is a chunk of code bounded by {}. A block lives in curly braces. Anything within curly braces is a block. So a variable declared in a block with
We see that using let can be updated but not re-declared.Just like
this will return an error:
However, if the same variable is defined in different scopes, there will be no error:
Why is there no error? This is because both instances are treated as different variables since they have different scopes. This fact makes Also, since a variable cannot be declared more than once within a scope, then the problem discussed earlier that occurs with Hoisting of letJust like ConstVariables declared with the const declarations are block scopedLike
const cannot be updated or re-declaredThis means that the value of a variable declared with
nor this:
Every This behavior is somehow different when it comes to objects declared with
while we cannot do this:
we can do this:
This will update the value of Hoisting of constJust like So just in case you missed the differences, here they are:
Got any question or additions? Please let me know. Thank you for reading :) Learn to code for free. freeCodeCamp's open source curriculum has helped more than 40,000 people get jobs as developers. Get started Should I use var let or const?As a general rule, you should always declare variables with const, if you realize that the value of the variable needs to change, go back and change it to let. Use let when you know that the value of a variable will change. Use const for every other variable. Do not use var.
What is the difference between Let & const?let have the block scope. const variable has the block scope. It gets hoisted to the top of its scope and initialized undefined. It also got hoisted to the top of its scope but didn't initialize.
Why is let and const better than VAR?let can be updated but not re-declared.
This is because both instances are treated as different variables since they have different scopes. This fact makes let a better choice than var . When using let , you don't have to bother if you have used a name for a variable before as a variable exists only within its scope.
|