Javascript var,let,const
- Suneetha Yamani
- Feb 17, 2022
- 2 min read
As a developer we should learn how to declare variables.
Before ES6 of javascript var keyword was used to declare varibales. With ES6 we got two new key words for variable declaration let and const. How these keywords different from var.Most of developers will use interchanged these keywords.
How to declare variables in Javascript
With out these keywords it will be same as var,but it is not allowed in strict mode.

Scope of Variable in Javascript
There are mainly 3 types of scope:
Block scope
Functional scope
Global scope
How to use in Block Scope

if we use var inside block scope it will override ,so always use let or const for block scope.
How to use in Function Scope

As you see none of the variables accessible outside function.So variables declared inside function is not accessible outside of it.
How to use in Global Scope

As you see variable is accessible every where.
var : Functional scope level
let:Block scope level
const: Block scope level
How to reassign new value to Variable in JS
Once you define a variable using var or let, we can reassign value to variable.But with const,we can't reassign anew value at all.

There is a tricky part with cost that you must be aware of. When an object is declared and assigned a value with const, you can still change the value of its properties. But you can not reassign another object value to the same variable. This is a common mistake many developers make.
What Happens When You Access a Variable Before Declaring it in JavaScript
As a programmer, you should never try accessing a variable without declaring it.
If you access before declare we will get
Var: undefined
let:Reference error
Const:Reference error
So please follow below rules:
Don't use var anymore.
Use let or const.
Use const more often. Use let when you need to reassign another value to a variable.
Don't try to access a variable without declaring it.
Thank you friends :)



Comments