JavaScript Are named callbacks function expressions?

Given the following example:

setTimeout(function name(){console.log(1)}, 1000)  (callback)

Is name() (the callback) considered a function declaration or a named function expression?

I know that function declarations are hoisted and function expressions are not, but because it is within the context of an argument, how would you determine whether you can hoist it or not?

I don't believe it to be a duplicate of what is the difference between function declarations and function expressions.

I'm asking a specific usecase of whether or not named callback functions are function declarations or function expressions. I do not believe this usecase to be covered within the answers provided within that question.

Answer:1

Generally speaking, if a function definition is in an expression position, then it's a function expression.

When is something an "expression position"? When you cannot put a statement there.

Arguments are expressions (you would not be able to pass an if statement as an argument) therefore function name() {...} is an expression in your example.

And because it is a function expression you could omit the name.


Now you might be wondering about the following: Assuming you know that 5 + 5 is an expression and that you can put 5 + 5 in a line just like so

5 + 5;

it seems we can put expression anywhere we want. So how does JavaScript decide, given

function name() {
  //...
}

whether it is a function declaration or a function expression?

To answer this question we have to look at how the code is structured in general. At the top level, all you have is a list of statements:

<statement1>
<statement2>
<statement3>
...

There are various types of statements, an if statement is one example. There is also the concept of an ExpressionStatement. As the name implies, it's a statement that consists of a single expression. This is the rule that allows you to put 5 + 5; in that list of statements.

But this parsing rule is restricted. It cannot start with the keyword function (among others). Therefore, when JavaScript encounters the above function definition, it won't be parsed as a function expression, because it cannot parse it as an ExpressionStatement. The only choice left is to parse it as a function declaration.


If you are curious about the structure of a JavaScript program you can use https://astexplorer.net (full disclosure: I built this site) to see how popular JavaScript parsers pare the program.

For example,

setTimeout(function name(){console.log(1)}, 1000)

is parsed to

enter image description here

by acorn.

(you should really look at the live version though, because hovering over the AST highlights the corresponding source text, which makes things easier to understand)

Answer:2

I have a Backbone.Marionette item view. It renders a list of items. I want to listen for the scoll event everytime somebody scrolls the list. I assumed this would work: events: { 'scroll ul': '...

I have a Backbone.Marionette item view. It renders a list of items. I want to listen for the scoll event everytime somebody scrolls the list. I assumed this would work: events: { 'scroll ul': '...

  1. capture scroll event javascript
  2. capture scroll event angular
  3. capture scroll event
  4. capture scroll event jquery
  5. capture scroll event angular 4
  6. capture scroll event js
  7. capture scroll event angularjs
  8. capture scroll event div
  9. react capture scroll event
  10. iframe capture scroll event
  11. capture mouse scroll event
  12. capture window scroll event
  13. extjs capture scroll event
  14. excel vba capture scroll event

I would like to use a javascript loop to create multiple HTML wrapper elements and insert JSON response API data into some of the elements (image, title, url, etc...). Is this something I need to go ...

I would like to use a javascript loop to create multiple HTML wrapper elements and insert JSON response API data into some of the elements (image, title, url, etc...). Is this something I need to go ...

  1. javascript using loop
  2. in javascript loop
  3. javascript in loop array
  4. javascript using while loop
  5. create array using loop javascript

I'm trying to read and add multiple files to an array. I've already found out that readAsArrayBuffer is a asynchronous function, so i need to wait for previous uploads to end. I tried to use callbacks ...

I'm trying to read and add multiple files to an array. I've already found out that readAsArrayBuffer is a asynchronous function, so i need to wait for previous uploads to end. I tried to use callbacks ...

  1. multiple file reading in java
  2. python multiple file reading
  3. reading multiple file in r

I am facing issues in writing into iframe, I am able to identify the iframe but I am not able to write anything into the frame. Whenever I try to write into the frame I am getting below error. org....

I am facing issues in writing into iframe, I am able to identify the iframe but I am not able to write anything into the frame. Whenever I try to write into the frame I am getting below error. org....

  1. need help writing a paper
  2. need help writing a book
  3. need help writing an essay
  4. need help writing a song
  5. need help writing a resume
  6. need help writing a letter
  7. need help writing a poem
  8. need help writing a cover letter
  9. need help writing vows
  10. need help writing my resume
  11. need help writing a sentence
  12. need help writing business plan
  13. need help writing rap lyrics
  14. need help writing a rap song
  15. need help writing my book
  16. need help writing a thank you note
  17. need help writing research paper
  18. need help writing my vows
  19. need help writing dissertation
  20. need help writing a love letter