All you need to know about var, let & const in JavaScript
Tags: javascript, webdev, beginners, tutorial
Published: 2021-08-20 06:56:26

In JavaScript a variable can be defined using the keywords **`var`**, **`let`**, or **`const`**.

Before we can understand how **`var`**, **`let`**, and **`const`** differ, we need to understand a computer science-y concept called scope.
Scope essentially means where these variables are available for use. 

{% youtube nqBeKc_kMtU %}

###Global Scope
Variables declared Globally (outside any function) have Global Scope.
Global variables can be accessed from anywhere in a JavaScript program.


###Function Scope
Variables declared Locally (inside a function) have Function Scope.
Local variables can only be accessed from inside the function where they are declared.

###Block Scope
A block of code is the code between curly braces in JavaScript.
Variables declared inside a block {} have block scope.
>`Variables declared with the var keyword cannot have Block Scope.`

{% youtube HFOXWsNLCLA %}
###var

`var` declarations are globally scoped or function/locally scoped.
The scope is global when a `var` variable is declared outside a function.
var is function scoped when it is declared within a function. 

![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qb49do07vurg4qmimhu6.png)
 

 variables declared with `var` keyword can be re-declared like this
![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lebh7btfvkrrtxclbrm0.png)

or their value can be updated like this 
![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hjq88gk63qzr4kmze6c6.png)


###let

 `let` is now preferred for variable declaration. It's no surprise as it comes as an improvement to `var` declarations. It also solves the problem with `var`. Let's consider why this is so.
`let` is block-scoped, so a variable declared in a block with `let` is only available for use within that block.
 
![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/45ameiz4jnyhox6ljjyl.png)

variables declared with `let` keyword cannot be re-declared it will throw error like this 
![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/g3o3gcbyemc6uwk4ioox.png)
 
`let` variables can be updated within its scope like this
![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bz9rr9orazr2zx18f8lg.png)
 

###const

Variables declared with the `const` maintain constant values. `const` declarations share some similarities with `let` declarations.
Like let declarations, `const` declarations can only be accessed within the block they were declared.

![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ykn6c5tappk4yfxg6vip.png)
 
But variable declared with `const` can neither be re-declared nor re-assigned

![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8hrfd72cq8l7gidjsu9n.png)

![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/s96n4rw5taegaotgd2fv.png)
   
 Thanks for reading.

"Don't miss out" Follow my Social handles👉
[Subscribe my YouTube channel😊](https://www.youtube.com/mayanktyagi)
[Instagram😊](https://www.instagram.com/the_cs_guy_/) **||** [Twitter😊](https://twitter.com/tmayank860)

If you find this helpful and want to support💲 [Buy Me Coffee☕](https://www.buymeacoffee.com/mayanktyagi)