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 Callback Functions in JavaScript Introduction to Callback Functions Creating an Anonymous Callback Function

Convert log in to an anonymous function and pass the anonymous function directly into functionRunner.

//The app.js file originally looked like this:
function log() {
  console.log("Hello World!");
}

functionRunner(log);

//My edit on the app.js file was this:
function log( callback ) {
  callback();
}

log(() => console.log("Hello World!"));

functionRunner(log);

//I am confused as to where to fix this... The error I received was as follows: "There was an error with your code: TypeError: 'undefined' is not a function (evaluating 'callback()')"
app.js
function log( callback ) {
  callback();
}

log(() => console.log("Hello World!"));

functionRunner(log);
index.html
<!DOCTYPE html>
<html lang="en">
    <head>
        <title></title>
        <link rel='stylesheet' href='styles.css'>
    </head>
    <body>
        <section>
            <p>Open your browser's console to see the results</p>
        </section>
        <script src='runner.js'></script>
        <script src='app.js'></script>
    </body>
</html>
runner.js
function functionRunner(callback) { 
  callback();
}

1 Answer

Simon Coates
Simon Coates
8,225 Points

It seemed to accept:

functionRunner(function() {
  console.log("Hello World!");
});

But that's not an anonymous function... is it??

Simon Coates
Simon Coates
8,225 Points

MDN (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions) describes the following as an anonymous function and I saw similar example at w3cschools.

var myFunction = function() {
    statements
}

I think most people would immediately start to think about arrow syntax. But I tried this an it worked and it didn't seem to like my use of arrow syntax, so I guess that was what it wanted. The language i used was "It seemed to accept", which avoids committing to anything beyond passing the challenge.