Variables scope in Javascript
Tags: javascript, webdev
Published: 2020-05-03 13:40:04

Javascript variables are a container for storing values. In javascript, there are three types of variables `let` `const` and `var`.

`var` variable has a functional scope which means that variables that are defined inside a function can only be accessible within the function.
![var functional scope](https://dev-to-uploads.s3.amazonaws.com/i/v1swknn8h48stz73pzy4.png)

`var` type variables don't have block scope they are accessible outside of the block
![var block scope](https://dev-to-uploads.s3.amazonaws.com/i/p97cw8ynz35i5g77qrxh.png)

`var` type variables override `var` type variable which has the same name in their scope.
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/fsrtqefcxuyr1ouutf4l.png)

Redeclaring a `let` or `const` variable with `var` is not allowed in the same scope and in functional scope 
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/emxkeu1igj1i00fx5zwi.png)

`let` type variable has the same functional scope as `var` variable but they also have `Block sope`
![let functional scope](https://dev-to-uploads.s3.amazonaws.com/i/zait3hzyk5v6w1b1iw82.png)

`let` type variable defined in `Block` are `NOT` accessible outside of `Block` as shown in the example.
![let block scope](https://dev-to-uploads.s3.amazonaws.com/i/v8lok1txcws4odq8h0zj.png)

Redeclaring a `let` variable with type `let` or `const` or `var` is not allowed in the global. The compiler will throw an error
![let variable global scope](https://dev-to-uploads.s3.amazonaws.com/i/2096lkfa136qsy1unhyi.png)

Redeclaring a `let` variable with type `let` or `const` or `var` is not allowed in the block. The compiler will throw an error
![let variable block scope](https://dev-to-uploads.s3.amazonaws.com/i/lb1161lqzm9g18srdsha.png)

Redeclaring a `let` variable with type `let` or `const` or `var` is not allowed in the functional scope. The compiler will throw an error
![let variable function scope](https://dev-to-uploads.s3.amazonaws.com/i/79i5vpncx0j78zbb2aua.png)


Now let's talk about `const` variables. As the name suggests we cannot reassign a value to the `const` variable.
![const variable](https://dev-to-uploads.s3.amazonaws.com/i/k7l00yr6qgz39j1vgf5f.png)
 
`const` type variable also have `functional` and `Block` level scope.
![const functional scope](https://dev-to-uploads.s3.amazonaws.com/i/rdlmm9gtdh9hhnw7bnpi.png)


`const` type variable defined in `Block` is `NOT` accessible outside of block as shown in the example.
![const block scope](https://dev-to-uploads.s3.amazonaws.com/i/2p7u2i09v9ukkb2tp266.png)

Redeclaring a `const` variable with type `let` or `const` or `var` is not allowed in the global. The compiler will throw an error
![const variable global scope](https://dev-to-uploads.s3.amazonaws.com/i/mk5ekhywyka9dyz3gkri.png)

Redeclaring a `const` variable with type `let` or `const` or `var` is not allowed in the block. The compiler will throw an error
![const variable block scope](https://dev-to-uploads.s3.amazonaws.com/i/6azpc5k26z5oq0es7msa.png)

Redeclaring a `const` variable with type `let` or `const` or `var` is not allowed in the functional scope. The compiler will throw an error
![const variable functional scope](https://dev-to-uploads.s3.amazonaws.com/i/xyk9lof1gm9lu6jmtmlo.png)