Variables in JavaScript
Are you struggling with variables in JS and their Declaration like let, const, var? Just read this blog I have Explained everything in simple words.

I am a Frontend Web Developer and a self-Learner. I want to utilize my skills for Development. I want to work in a competitive environment where I can enhance my skills along with facing the new Situations, learning new things, Exploring the world.
Introduction
Hello guys ๐! I hope you are good then let's start with a new topic which is very basic topic for the web developers but some of them do not know what is the difference between let, const and var.
Let's Discuss this topic on the basis of few points -
When let and const introduced in JavaScript?
Basic Introduction
Redeclaration & Reinitialization of variables.
Scope of var, let and const.
Access before Declaration
Conclusion
When let and const introduced in ECMAScript?
let and const introduced in JavaScript in ES6 Model released in 2015. Before 2015 Developers were using var keyword only for variable declaration. But now we have two more methods to declare variable.
Basics
In JavaScript there are three methods to declare a variable using var, let, const keywords.
var name = "niket";
let name = "niket";
const name = "niket";
Redeclaration & Reinitialization of variables.
var
if we declare a variable using var keyword then we can Redeclare and Reinitialize it.
var myName = "niket";
console.log(myName)
var myName = "prince";
console.log(myName)
Output - niket
prince
let
if we declare a variable using let keyword then we can update the value, but we cannot Redeclare if we trying to do this then we will be getting syntaxError.
let myName = "niket";
console.log(myName)
let myName = "prince";
console.log(myName)
Output -

const
if we declare a variable using const keyword then we cannot Redeclare and cannot update the value if we try to do this then we will be getting syntaxError.
const myName = "niket";
console.log(myName)
const myName = "prince";
console.log(myName)
Output -

we have to initialize the variable also during declaration otherwise we will get syntaxError.
const myName;
myName = "niket"
console.log(myName)

Scope of variables.
var
The scope of the variable is global i.e., we can access the variable anywhere in the program or function/locally scope, which is declare only in function, its scope is limited to that function, and it will release from memory after function execution completed.
if we declared a variable globally named ``` myName
var myName = "niket"; var age = 14;
if (age > 10) { var myName = "prince"; }
console.log(myName) // "prince"
output - prince
### let and const
a variable declared in a block with **let or const** keyword is only available for use within that block. Let me explain this with an example:
if we declared a variable globally named ```
myName
``` and we declare same variable in a block like (if-else) then it will create a new variable which has only block scope and we cannot access block variable outside the block.
let myName = "niket"; let age = 14;
if (age > 10) { let myName = "prince"; console.log(myName) // prince }
console.log(myName) // "niket"
output - prince niket
## Access before Declaration
### var
We **can access the variable before its initialization**, but **we will be getting a special value** ```
undefined
```, and this is happening because of ***hoisting***. We will discuss about hoisting in detail in the next blog.
console.log(name)
var name = "niket";
output - undefined
### let and const
We **cannot access the variable before its initialization** in case of let and const. if we try to do this we will be getting **ReferenceError**.
console.log(name)
let name = "niket"; //const name = "niket";

# Conclusion
- **var declarations are globally scoped or function scoped** while **let and const are block scoped**.
- **var variables can be updated and re-declared** within its scope. **let variables can be updated but not re-declared**, and **const variables can neither be updated nor re-declared**.
- While **var and let can be declared without being initialized**, **const must be initialized during declaration**.
Whoa! I think this is little bit confusing but if you have read this blog carefully then I hope it will help you to understand behavior of variables in JavaScript and play more with this by yourself then you will better understand about it.
We will also discuss **hosting** and** how does hoisting work** differently in let, var and const? and What is **temporal dead zone** in another blog.




