Var, let 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, ADVERTISEMENT 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 ADVERTISEMENT 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 ADVERTISEMENT 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 ADVERTISEMENT 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 What is difference between VAR let and const in JavaScript?Variables declared with var are in the function scope. Variables declared as let are in the block scope. Variables declared as const are in the block scope.
Should I use VAR or let or const in JavaScript?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.
Where can I use let const and VAR?If you do not want a variable declared inside a { } block to be accessed outside of the block, you need to declare them using the let or const keywords. Variables declared with the var keyword inside the { } block are accessible outside of the block too. So, be careful.
What are the main differences between LET and const?JavaScript has three variable declaration statements: var , let and const . The latter two were added in ES6, whereas var existed since previous versions. One of the first things to notice is that const defines constants (i.e. values that will not be reassigned), whereas var and let define variables.
|