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 Asynchronous Programming with JavaScript Asynchronous JavaScript with Callbacks Implement a Callback

Begana Choi
PLUS
Begana Choi
Courses Plus Student 13,126 Points

still a bit confused about using genarateHTML function as an argument without argument

the video said it's because doesn't want to execute generateHTML immediately but I can't get it why it's not executed immediately when it doesn't have an argument. can somebody explain easier? thank you.

3 Answers

It's not about the argument, it's about the parentheses ().

A function can either be declared (the moment where you define function). Which usually looks like this:

function myFunc () {
   return "Hello World"
}

or you can call a function which usually looks like this

myFunc()

or, you can refer to a function, which looks like this

myFunc

The last option is not calling the function, it's simply mentioning it. It's basically pointing to the function.

Why would this be useful you may think?

It's useful because in javascript, you are allowed to use functions like normal variables. This means you can pass a function as an argument to another function. Or a function can return a function.

This will allow you to do some cool things like this

function calculate(operation, firstNumber, secondNumber) {
  return operation(firstNumber, secondNumber)
}

function add(firstNumber, secondNumber) {
  return firstNumber + secondNumber
}

function subtract(firstNumber, secondNumber) {
  return firstNumber - secondNumber
}

console.log(calculate(add, 3, 6)) // 9

note how we passed a reference to the function add without calling it, we called it inside the calculate function.

Wouldn't sipmly

console.log(add(3, 6))

Achieve the same thing?