3.1 KiB
3.1 KiB
title |
---|
Compare Scopes of the var and let Keywords |
Remember to use Read-Search-Ask
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 withlet
.
try to solve the problem now
- Add
let
to the variablei
inside of your if statement.
try to solve the problem now
Spoiler Alert!
Solution ahead!
Basic Code Solution:
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;
}
Code Explanation:
By using let
you can declare variables in relation to their scope.
Relevant Links
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
Wiki Challenge Solution Template
for reference.