Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript Introducing ES2015 ES2015 Basics Let and Const

Samuel Kleos
seal-mask
.a{fill-rule:evenodd;}techdegree
Samuel Kleos
Front End Web Development Techdegree Student 13,674 Points

More detail . . . 🧐 Where is the function scope? Declaration or Execution?

I don't understand where the accessible scope of doLoop(x) lies.

Does the doLoop(x) functions' accessible scope depend on where it's called / executed? i.e. inside the for loop?

Or is the doLoop(x) function have access to variables that are in the scope of where it's declared? i.e. in the top level of the initLoop() function?

From what I understand, the function will have access to variables and other functions within itself and outside itself in the scope of where it's declared.

'use strict';

(function initLoop() {
  function doLoop(x) {
    console.log('loop:', x);
  }

  for (var i = 0; i < 10; i++) {
    doLoop(i + 1);
  }
})();

1 Answer

Steven Parker
Steven Parker
231,269 Points

If by "outside itself" you are including all wider scopes (such as global scope), then it sounds like you have it. :+1:

You might want to try a few experiments to prove to yourself that variables in the execution scope will show up as "undefined".