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 JavaScript and the DOM (Retiring) Responding to User Interaction Delaying Execution with setTimeout()

Joseph Michelini
.a{fill-rule:evenodd;}techdegree seal-36
Joseph Michelini
Python Development Techdegree Graduate 18,692 Points

Does passing an arrow function expression for the first time as an argument to a method mean that I can't use it later?

For example, if I type:

window.setTimeout((something) => {
}, 3000, 'Greetings Everyone!');

as opposed to:

const greeting = function(something) {

followed by the window.setTimout code above, with greeting as the first argument (as opposed to the arrow expression), or even:

const greeting = greeting => {console.log(greeting)}

...there's no way to "quickly" assign my greeting function, correct (referring to the code at the top)? Though I'm led to believe the "best practice" is to avoid declaring variables when you don't have to?

Thanks in advance! Just trying to figure out why & when with all of the above.

2 Answers

Tim Oltman
Tim Oltman
7,718 Points

Hi Joseph,

Passing an anonymous function as an argument means you will not be able to use it later. If you want to use the function later there is nothing wrong with using a function declaration or assigning the function expression to a variable. We use anonymous functions and arrow functions more for convenience and clarity than performance.

Technically, functions will take the value of an assignment expression as an argument. For example, the following code will both assign x the value 4 and pass 4 into the console.log function:

var x;

console.log(x = 2 + 2) // 4
console.log(x) // 4

Therefore, you could assign your arrow function to a variable as you passed it as an argument.

var greeting;

setTimeout(greeting = (something) => {
}, 3000, 'Greetings Everyone!')


In this case you would see "hi" printed to the console followed by "Greetings Everyone!" However, you probably shouldn't do that. Your code will be much clearer if you simply assign or declare your function first.

Doron Geyer
Doron Geyer
Full Stack JavaScript Techdegree Student 13,884 Points

Hey Joseph! The short answer is no you would not be able to reuse it without declaring the function first. If you wanted to use it again you would need to use something like this:

function say(something){

window.setTimeout(say,5000, "shazam!");

say('this happens first');

Here you would get the alert saying "this happens first" followed by the execution five seconds later with the alert saying "shazam!"