0

Assuming a function declaration is a statement where the function keyword is the first word of the statement, e.g.:

function() { console.log("foo") };

Assuming that a function expression is e.g. following:

for a named function

var func = function doSomething() { console.log("foo") };

for an anonymous function

var func = function() { console.log("foo") };

What is the case for the anonymous function, which is passed in as parameter in the following example:

for (let i = 0; i < 5; i++) {
    setTimeout(function() { console.log(i); }, 200); 
};

Is that a function declaration or is it a function expression since it is getting assigned to a parameter-variable of the setTimeout-method of WindowOrWorkerGlobalScope

quizmaster987
  • 549
  • 1
  • 6
  • 15
  • 2
    They are not statement, therefore, they are expressions. – VLAZ May 27 '20 at 06:47
  • You can't write a statement within another statement; it can only be an expression. – deceze May 27 '20 at 06:48
  • # Response if you really what to know that you need to have a clear understanding of how the javascript runtime engine works. ## Detailed Video https://youtu.be/QyUFheng6J0 – Ernesto May 27 '20 at 07:26
  • `var func = …;` is not a function expression, it's an variable declaration with an initialiser - a statement. Only the `function() {…}` part is an expression in there. And yes, it's the same in an argument position. – Bergi May 27 '20 at 07:44

1 Answers1

0

Clearly a function expression

From MDN

Function expression

The function keyword can be used to define a function inside an expression.


Syntax

let myFunction = function [name]([param1[, param2[, ..., paramN]]]) {
   statements
};

A function expression is very similar to and has almost the same syntax as a function declaration (see function statement for details). The main difference between a function expression and a function declaration is the function name, which can be omitted in function expressions to create anonymous functions. A function expression can be used as an IIFE (Immediately Invoked Function Expression) which runs as soon as it is defined. See also the chapter about functions for more information.



Function declaration

Syntax

function name([param[, param,[..., param]]]) {
   [statements]
}
yunzen
  • 32,854
  • 11
  • 73
  • 106