1

I have a question about this code below:

function myfunc () {
return 2 + 2;
}

console.log(myfunc);

Does anyone know why, when we log 'myfunc' to the console, we get the entire function itself back? Or in other words, is 'myfunc' acting as a variable that holds the function's contents, or is it just referencing that function?

Because if I go ahead & add this to the code...

myfunc = undefined; //or any other value like myfunc = 20;

...then since myfunc's value is changed, I can no longer use it to invoke the function. So what is 'myfunc' really?

Bail3y
  • 51
  • 5

1 Answers1

2

The answer is yes, a function declaration creates a symbol in the local function scope (or global scope if the declaration is in that context) that works exactly like a variable declared with var (though function declarations are hoisted above var declarations).

Now, a function expression like this:

var x = function helloWorld() { return "hello world"; };

does not create a local "helloWorld" symbol (except when it does). The value of a function expression is a reference to the created function, and that can be assigned to a variable just like any other value.

Pointy
  • 405,095
  • 59
  • 585
  • 614
  • Thanks, that answers my question. So would you say a function declaration is also, in a way, an expression? Since the function's name resolves to the function? – Bail3y May 07 '17 at 22:58
  • 1
    No. A function declaration statement is **not** an expression. When the `function` keyword is the first token in a statement, it's a function declaration, and the main effect that has on coding is that the function cannot be immediately invoked. – Pointy May 07 '17 at 23:01
  • Ok that definitely makes sense now. I just don't get 1 thing, sorry. If a function declaration creates a symbol that works exactly like a variable...then aren't we saying the functions name resolves to the function itself? If we are, then doesn't that mean we have an 'expression' where the name evaluates the function? I know you just said no, but I just don't get the reasoning. If we have a variable that evaluates to the number 1, that's an expression, yet if we have a function declaration that has a name, who evaluates to the function, it's not an expression? What am I missing... – Bail3y May 07 '17 at 23:20
  • 2
    Functions are objects, and in JavaScript the "value" of an object is a *reference* to the object. So the value of a function name in its declaration context is a reference to the function object that the declaration created. It can be assigned to other variables or object properties or passed as an argument to a function. The function declaration is not an expression in the same way that a `var` declaration is not an expression: it simply *isn't* according to the rules of the language. The declaration *creates* a new symbol and gives it a value, but it's not itself an expression. – Pointy May 07 '17 at 23:32
  • Note that the term "expression" has a precise definition; it's not just a colloquial term. It's a specific part of the language syntax. – Pointy May 07 '17 at 23:32
  • To put it another way: asking "Is a function declaration statement an expression?" is like asking "Is a function declaration a `while` loop?". It just isn't. – Pointy May 07 '17 at 23:34
  • Interesting I understand 100% now, thanks again Pointy! But when you said a var declaration is not an expression, that confused me. Declaring var x = 2 + 2; is not an expression? – Bail3y May 07 '17 at 23:42
  • 1
    No. `2 + 2` is an **expression**, `var x = 2 + 2;` is a **statement**. If you want a crystal clear explanation, read *JavaScript: The Definitive Guide* by **David Flanagan**. – Badacadabra May 08 '17 at 00:14