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

Anonymous Functions

Can anyone help with this challenge:

On about line 18, make the anonymous function with the code 'window.didExecute = true' execute.

<script>
      var anonymousFunction = function() {

      };

      (function () {
        window.didExecute = true;
      });
    </script>

thanks.

2 Answers

You need to concentrate on this part of the code:

(function () {
    window.didExecute = true;
});

Your task is to create what is known as IIFE (immediately-invoked function expression).

This code is a function, but because it's an anonymous one, you can't execute it by calling its name, and it won't get executed on its own. However, it's still a function and same rules apply. To call a function, you'd add the parentheses after its name.

So, all you're supposed to do is add () before the final semicolon.

Thanks Dino, the way you explained it made it easy to understand!