Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript Asynchronous Programming with JavaScript Asynchronous JavaScript with Callbacks Implement a Callback

Begana Choi
PLUS
Begana Choi
Courses Plus Student 13,122 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?