Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript

M Khan
M Khan
7,024 Points

Why do we need to put outer function in a variable, why we can't just call the dogHouse function directly ?

var birds = 3;

function dogHouse(){
  var dogs = 8;
  function showDogs(){
    echo(dogs);
  }
  return showDogs;
}

var getDogs = dogHouse();

getDogs();

1 Answer

Steven Parker
Steven Parker
221,328 Points

The "dogHouse" function returns another function, and the code is calling that other function.

But you could do it like this: "dogHouse()();" — but that looks really strange!

M Khan
M Khan
7,024 Points

Clear. Grazie signore