59 lines
3.1 KiB
Markdown
59 lines
3.1 KiB
Markdown
![]() |
---
|
||
|
title: Compare Scopes of the var and let Keywords
|
||
|
---
|
||
|

|
||
|
|
||
|
 Remember to use <a>**`Read-Search-Ask`**</a> if you get stuck. Try to pair program  and write your own code 
|
||
|
|
||
|
### Problem Explanation:
|
||
|
|
||
|
We need to change `var` to `let` in our function scope and add `let` to our block scope.
|
||
|
|
||
|
##  Hint: 1
|
||
|
|
||
|
* Find `var` and replace with `let`.
|
||
|
|
||
|
> _try to solve the problem now_
|
||
|
|
||
|
* Add `let` to the variable `i` inside of your if statement.
|
||
|
|
||
|
> _try to solve the problem now_
|
||
|
|
||
|
## Spoiler Alert!
|
||
|
|
||
|

|
||
|
|
||
|
**Solution ahead!**
|
||
|
|
||
|
##  Basic Code Solution:
|
||
|
```javascript
|
||
|
function checkScope() {
|
||
|
"use strict";
|
||
|
let i = "function scope";
|
||
|
if (true) {
|
||
|
let i = "block scope";
|
||
|
console.log("Block scope i is: ", i);
|
||
|
}
|
||
|
console.log("Function scope i is: ", i);
|
||
|
return i;
|
||
|
}
|
||
|
```
|
||
|
 <a href='https://codepen.io/dylantyates/pen/wxwxRd' target='_blank' rel='nofollow'>Run Code</a>
|
||
|
|
||
|
# Code Explanation:
|
||
|
|
||
|
By using `let` you can declare variables in relation to their scope.
|
||
|
|
||
|
#### Relevant Links
|
||
|
|
||
|
* <a href='https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/let' target='_blank' rel='nofollow'>let</a>
|
||
|
|
||
|
##  NOTES FOR CONTRIBUTIONS:
|
||
|
|
||
|
*  **DO NOT** add solutions that are similar to any existing solutions. If you think it is **_similar but better_**, then try to merge (or replace) the existing similar solution.
|
||
|
* Add an explanation of your solution.
|
||
|
* Categorize the solution in one of the following categories — **Basic**, **Intermediate** and **Advanced**. 
|
||
|
* Please add your username only if you have added any **relevant main contents**. ( **_DO NOT_** _remove any existing usernames_)
|
||
|
|
||
|
> See  <a href='http://forum.freecodecamp.com/t/algorithm-article-template/14272' target='_blank' rel='nofollow'>**`Wiki Challenge Solution Template`**</a> for reference.
|