Files
Randell Dawson 1494a50123 fix(guide): restructure curriculum guide articles (#36501)
* fix: restructure certifications guide articles
* fix: added 3 dashes line before prob expl
* fix: added 3 dashes line before hints
* fix: added 3 dashes line before solutions
2019-07-24 13:29:27 +05:30

1.3 KiB

title
title
Compare Scopes of the var and let Keywords

Compare Scopes of the var and let Keywords


Problem Explanation

Change the code so that the variable i declared in the if block is separately scoped than the variable i declared at the beginning of the function.


Hints

Hint 1

  • Be certain not to use the var keyword anywhere in your code.

Hint 2

  • Remember that let's scope is limited to the block, function or statement in which you declare it.

Solutions

Solution 1 (Click to Show/Hide)
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.