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 JavaScript Foundations Functions Anonymous Functions

Don Shipley
Don Shipley
19,488 Points

JavaScript

No matter what I try it will not pass http://teamtreehouse.com/library/javascript-foundations/functions/anonymous-functions http://teamtreehouse.com/library/anonymous-functions On about line 18, make the anonymous function with the code 'window.didExecute = true' execute.

  var anonymousFunction = function(){};
// line 18 below
      (function () {
        window.didExecute = true;
      });

3 Answers

While it may look strange at first, you execute anonymous functions the same way you execute any other function, by adding the parentheses at the end of the function. When it comes to named functions, you add the parentheses after the function name to execute it like this:

someFunction();

An anonymous function obviously doesn't have a name, so you add the parentheses after the function body, like this:

(function () {
   // some code
})();

This is something called IIFE — Immediately-invoked function expression.

Don Shipley
Don Shipley
19,488 Points

var anonymousFunction = function () {};

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

Oops! It looks like Task 1 is no longer passing.

Look at my code again. There's only one semi-colon at the end of the function expression. You have an additional one in-between the parentheses.

Don Shipley
Don Shipley
19,488 Points

Thank you Dino... Tried that once before removing the ; gave me the same result. Maybe I had removed the wrong on but think I removed the same one before It passed this time..