For Beginners: What you need to know about Var, Let, Const
Tags: javascript, beginners, tutorial, computerscience
Published: 2020-05-08 04:04:02

I originally wrote a tutorial about Var, Let, and Const using [Observable Notebook](https://observablehq.com/@chi37/var-let-const-for-beginners).
In the notebook, you can edit the code, so that you understand by doing.

As a junior dev, it seems like you are told to never use var because the variable can be accessed anywhere, but what does that even mean? Keep reading and hopefully, understand the vocabulary and seeing code examples help. Of course, mess around with code as well.

## REASSIGN VS REDECLARE:&nbsp;
When you create a variable, you declare it with the keywords <code ${codeStyle}>let, var, const</code>. Reassign means you are giving the variable another value. <code>var</code> can both be redeclared and reassigned. You will see that it is different for <code>const and let</code> from this table.
![Table showing Scoping of Let Var and Const ](https://dev-to-uploads.s3.amazonaws.com/i/tgk6utfxmxmifss08wab.png)


```
  var color = 'blue'; //color declared
  color = 'black' //color reassigned
  var color = 'pink' //color redeclared
  return color
```

## SCOPE:
Var, let, and const have different types of scope. Scope is the context where variables and expressions live. 
#### Types of Scope
- <b ${vocabStyle}>&nbsp;Global:&nbsp;</b> Variables can be accessed anywhere. A JS document is globally scoped. <code ${codeStyle}>var</code> is globally scoped unless it is declared in a function scope.
- <b ${vocabStyle}>&nbsp;Local:&nbsp;</b> Variables in a function scope or block scope are local.
- <b ${vocabStyle}>&nbsp;Lexical:&nbsp;</b> A child scope has access to data in its parent's scope. Think nested functions - inner functions have access to variables of the outer function.
- <b ${vocabStyle}>&nbsp;Block:&nbsp;</b> Code enclosed in curly brackets <code ${codeStyle}>{}</code> can't be accessed outside the block. Variables declared with <code ${codeStyle}>let and const</code> have block scope. You see the block scopes in if/else statements, for example. <code ${codeStyle}>var</code> variables are *NOT* block scope.
- <b ${vocabStyle}>&nbsp;Function:&nbsp;</b> The variables declared in a function is accessible in that local scope. <code ${codeStyle}>var</code> is used to declare in functions, though you might be told to only use <code ${codeStyle}>let</code> from now on.
```
  //Global Scope
 let milkyWay = 'I am globally scoped, so you can access me anywhere!';
  
  { //Block Scope 
    var earth = 'Earth'; 
    const sun = 'Sun';
    let house = 'My house';
  }

  console.log(earth) //'Earth'
  // console.log(milkyway)  //'I am globally scoped, so you can access me anywhere!'
  //console.log(house) // will return Error bc house is in block scope
  console.log(sun) //will return Error bc sun is in block scope
```
## HOISTING
hoisting means that everytime you declare a variable, Javascript's engine will immediately hoist (or PULL) those variables up in the beginning of the scope and the variables will be declared. But will they be assigned or initialized? <br> With var, it will be initialized as undefined, while let and const will not. That is why you get an error
![Code Snipper of Hoisting example](https://dev-to-uploads.s3.amazonaws.com/i/jg3498ayt6ritcnotyfl.png)