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

Michael Criste
Michael Criste
5,045 Points

It says I'm missing parentheses at the end, but it matches my notes.

I've tried several formats. I don't think the one I have currently is correct. I don't think I need to have the variable name of the function at the very end of the anonymous function. How can I get this to pass. It says it's an issue with the parentheses at the end, but when I refer to my notes there doesn't seem to be anything wrong.

index.html
<!DOCTYPE html>
<html lang="en">
  <head>
    <title>JavaScript Foundations: Functions</title>
    <style>
      html {
        background: #FAFAFA;
        font-family: sans-serif;
      }
    </style>
  </head>
  <body>
    <h1>JavaScript Foundations</h1>
    <h2>Functions: Anonymous Functions</h2>
    <script>
      var anonymousFunction = function () {

      };

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

1 Answer

Hugo Paz
Hugo Paz
15,622 Points

Hi Michael,

You need to make the last anonymous function execute:

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

To make any function auto executable you wrap it in parenthesis and use a set of parenthesis at the end , passing any arguments there if necessary.

So the answer is:

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

To help you understand the concept:

//this
function test(){
console.log('test');
}
//is the same as this
var test = function(){
console.log('test');
}
//both are called like this
test();