Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
mbeaudru authored Sep 21, 2017
1 parent 9e3c996 commit 6246a0d
Showing 1 changed file with 10 additions and 6 deletions.
16 changes: 10 additions & 6 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,21 +103,21 @@ I recommend always declaring your variables with ```const``` by default, and wit
<th>Scope</th>
<th>Reassignable</th>
<th>Mutable</th>
<th><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/var#var_hoisting">Hoisting</a></th>
<th><a href="#tdz_sample">Temporal Dead Zone</a></th>
</tr>
<tr>
<th>const</th>
<td>Block</td>
<td>No</td>
<td><a href="#const_mutable_sample">Yes</a></td>
<td>No</td>
<td>Yes</td>
</tr>
<tr>
<th>let</th>
<td>Block</td>
<td>Yes</td>
<td>Yes</td>
<td>No</td>
<td>Yes</td>
</tr>
<tr>
<th>var</th>
Expand Down Expand Up @@ -193,7 +193,7 @@ myVar = 2;

- **let**

```var``` and ```let ``` are about the same, but ```let``` declared variables are *block scoped* and they are **not** hoisted.
```var``` and ```let ``` are about the same, but ```let``` declared variables are *block scoped* and they are **not** accessible before they are assigned.

Let's see the impact of block-scoping taking our previous example:

Expand All @@ -212,13 +212,17 @@ function myFunction() {
console.log(myVar); // Undefined, myVar is not accessible outside the function.
```

Now, what it means for *let* (and *const*) variables for not being hoisted:
<a name="tdz_sample"></a> Now, what it means for *let* (and *const*) variables for not being accessible before being assigned:

```js
console.log(myVar) // raises an error !
console.log(myVar) // raises a ReferenceError !
let myVar = 2;
```

By contrast with *var* variables, if you try to read or write on a *let* or *const* variable before they are assigned an error will be raised. This phenomenom is often called [*Temporal dead zone*](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/let#Temporal_Dead_Zone_and_errors_with_let) or *TDZ*.

> **Note :** Technically, *let* and *const* variables declarations are being hoisted too, but not their assignation. Since they're made so that they can't be used before assignation it intuitively feels like there is no hoisting, but there is. Find out more on this [very detailed explanation here](http://jsrocks.org/2015/01/temporal-dead-zone-tdz-demystified) if you want to know more.
- **const**

A ```const```, as well as ```let```, declared variables are *block scoped* and not hoisted, but they can't be reassigned nor re-declared afterwards.
Expand Down

0 comments on commit 6246a0d

Please sign in to comment.