0

In JavaScript (ECMAScript 5), functions are valued (they are told to be "first-class functions").

This allows us to use them as expressions (an expression is everything which produces a value, and can contain other expressions : var exp0 = (exp1) + exp2 - exp3.function(); is a grammar-correct statement).

In the code above, there are 8 expressions : exp0, exp1, (exp1), exp2, (exp1) + exp2, exp3, exp3.function() and (exp1) + exp2 - exp3.function().


Because functions can be used as expressions, the following code is correct :

var my_function_0 = function a() {} is a named function expression.

The following code is also correct :

var my_function_1 = function(){}` is an anonymous function expression.

Both are valued, both are values.


Now, consider the code below :

function requiredIdentifier() {}

It is NOT a "named or anonymous function expression", but a function declaration.


My question is :

Does a declared function have/produce a value ?

This question is equivalent to this one : Is a declared function an expression ? (even if it's not a named or anonymous function expression ?!)

JarsOfJam-Scheduler
  • 1,659
  • 1
  • 16
  • 40
  • 1
    Possible duplicate of [What is the difference between a function expression vs declaration in JavaScript?](http://stackoverflow.com/questions/1013385/what-is-the-difference-between-a-function-expression-vs-declaration-in-javascrip) – Heretic Monkey Sep 16 '16 at 21:32
  • yes it's value is assigned to it's name as an object. – Redu Sep 16 '16 at 21:32
  • 1
    after you declare it do `var someVar = requiredIdentifier` and then test if `someVar` is undefined ... it's not , it has the value of the function object – charlietfl Sep 16 '16 at 21:33
  • @MikeMcCaughan : I know the difference between expression and declaration (the hoisting), I'm not asking for it and I don't think it's a dupplicate thus. – JarsOfJam-Scheduler Sep 16 '16 at 21:47
  • @Lern-X Answers on that question (and its duplicate) answer this question. – Heretic Monkey Sep 16 '16 at 21:49
  • 1
    The two questions you pose are not equivalent. Yes, a function declaration creates a named value. No, it is not an expression. For example, in many languages you may define a singleton class. A class declaration is not an expression but you have created a value (a single object). – Mike Cluck Sep 16 '16 at 21:53
  • @MikeMcCaughan : please do not delete or ask for deleting this question if it would really be a dupplicate :/ , I'm reading the answers and they seem to be very interesting :/ – JarsOfJam-Scheduler Sep 16 '16 at 21:58
  • It takes more than me to close a question. If Bergi doesn't think it's a duplicate, then it's not a duplicate -- he has a gold badge in JavaScript and could close it with a single vote. I don't have a gold badge, so I can't. – Heretic Monkey Sep 16 '16 at 22:13

3 Answers3

4

Does a declared function have/produce a value?

Yes. Regardless what syntax is used to create the function, a function is a callable object (i.e. it implements an internal interface that makes it callable):

function a() {}
var b = function() {}
var c = (new Function()) // or some other expression that returns a function

All of the variables a, b and c hold a function value.

The difference between the syntaxes is only when the value is created and whether/when it is bound to a variable. See var functionName = function() {} vs function functionName() {} for those details.

Community
  • 1
  • 1
Bergi
  • 513,640
  • 108
  • 821
  • 1,164
  • But what about the value in a declared function ? In an expression function, the value is the function itself and is bound to a variable. But in a declared function : what's the value ? And is it bound to the function identifier ? – JarsOfJam-Scheduler Sep 16 '16 at 22:13
  • The value is still the function itself, and it's bound to a variable as well. The difference is that the function declaration also declares the variable, and that the variable is initialised with the function on creation not with `undefined` like a `var`. – Bergi Sep 16 '16 at 22:24
  • What's the name of this variable created by the function's declaration ? Is it the function's name ? :o – JarsOfJam-Scheduler Sep 16 '16 at 22:26
  • @Lern-X Yes! In the example, the variable `a` is created by the function declaration. It works quite the same as `var a` (in terms of scope, assignability, writability, etc), except for the different initialisation. – Bergi Sep 16 '16 at 22:27
  • I trust you, but could you show me some documentation please ? I'm really surprised haha ^^ – JarsOfJam-Scheduler Sep 16 '16 at 22:31
  • @Lern-X you can read [the spec](http://es5.github.io/#x10.5) (notice how *CreateMutableBinding* is called both for variable declarations and function declarations), or you can just try it out. I don't think there's any other official "documentation" that details this. – Bergi Sep 16 '16 at 22:36
  • `CreateMutableBinding` is defined as creating a mutable binding according to the spec ; is this "mutable binding" a "mutable variable" ? I asked to other computer scientists : they think a declared function doesn't create a variable, even if the function's identifier looks like a variable. But you and the spec seem to really say that there is **really** a variable which is created ? So when we do this call : `declared_function();`, we are calling a variable named "declared_function" ? – JarsOfJam-Scheduler Sep 17 '16 at 11:05
  • Well, they seem to be wrong then (at least for JavaScript). Yes, a "mutable binding" is essentially a "mutable variable (for an identifier)". `a()` is no different from `b()` - it doesn't matter how the variable was created, only that it holds a function. – Bergi Sep 17 '16 at 11:07
  • So do you think the identifier `alert` is a variable which contains the function ? Thus, in JavaScript, every function is contained in a variable, even if it's an anonymous or not expression, or a declarated function ? And each function's call is in the shape of : "variable_name parenthesis" ? It's very surprising, it's the first time I hear that a declared function is stored in a variable. Until now, I thought the function's identifier was just a metadata bound to the function. But not a variable's identifier :/ ! – JarsOfJam-Scheduler Sep 17 '16 at 11:23
  • No, global functions are special, (`window.`)`alert` being a property of the global object. And no, not all functions are contained in variables, they can be properties or pure expression values (e.g. in an IIFE) as well. A call just needs to be any expression that resolves to a function value followed by parentheses with arguments. But yes, all local function declarations create a variable. Otherwise there would be no way to refer to them! – Bergi Sep 17 '16 at 12:35
  • Well, an object's property is not considered as a variable ? Last question ^^ and thank you for having answered me a lot of times ! – JarsOfJam-Scheduler Sep 17 '16 at 17:54
  • No, a property is part of an object and a variable is part of a scope. – Bergi Sep 17 '16 at 18:24
  • Btw, you might want to consider accepting the answer :-) – Bergi Sep 17 '16 at 18:24
  • Yes of course, I'm going to do that ! :) An object can't be considered as a scope, since it's a block ( { ... } ) ? Btw, I have a (really) last question : I didn't understand a very precise thing : when we call a function, the expression before the parenthesis is evaluated. The, identified function is invoked. But : what does it mean ? Is the function variable used ? So `foo()` would makes use of the function variable `foo` which contains the function. Is that correct ? Thus, function variables are quite different than other variables : without () they just are values. With (), they're exec. – JarsOfJam-Scheduler Sep 17 '16 at 20:25
  • No, block scopes and object literals are very different things even if they both use curly braces for their syntax. And no, there are no "function variables", they're just ordinary variables that happen to hold a function object. An expression without `()` just evaluates to a value, an expression with `()` it tries to call the value it evaluated to. The expression can contain a variable to evaluate, but it doesn't need to: `(function(){console.log("IIFE!")})()`. – Bergi Sep 17 '16 at 20:45
  • Ah, just : do you know what occurs when we use a named function expression : are there 2 variables which are created ? For example : `var foo = function hey(){}` : is a `hey` variable created, containing the function and then `foo` is also created and initialized to `hey` ? So why is it useful ? Anonymous function expressions would be satisfactory no ? – JarsOfJam-Scheduler Sep 17 '16 at 21:10
  • No, there is only one variable: `foo`. `hey` is the name of the function. For the usefulness, see [here](http://stackoverflow.com/questions/15336347/why-use-named-function-expressions) – Bergi Sep 17 '16 at 21:14
  • Heum in a declarated function, `hey` would be the function's name too (and its variable's name also), but ok ^^ – JarsOfJam-Scheduler Sep 17 '16 at 21:24
  • Yes, that's where function declarations differ from named function expressions. Btw, all of your questions should be answered by http://kangax.github.com/nfe/ – Bergi Sep 17 '16 at 21:26
2

A function declaration is a statement, not an expression. Since statements can't be used in expressions, it's pointless to ask what its value is, since there's no way to use the value.

However, the syntax of a function declaration is identical to that of a named function expression. So if you use it anywhere that an expression is required, it will be treated as a named function expression, and the value will be the function.

For example:

(function requiredIdentifier() {})

is a parenthesis expression containing a named function expression, not a function declaration. Its value is the function that was defined.

Barmar
  • 596,455
  • 48
  • 393
  • 495
  • I totally agree with you, and I already knew it. I understand it's quite pointless to ask myself if a declared function has a value, but I would want to know that because I'm interested in learning the intern functionment of JavaScript or ECMAScript : how is treated, in intern, a declared function ? Does it produce a value like an expression function ? – JarsOfJam-Scheduler Sep 16 '16 at 22:10
0

Well if you run the function requiredIdentifier() the returning value is that of undefined, and undefined is in fact a primitive data type in javascript (ecma script).

  • OP not asking about return values – charlietfl Sep 16 '16 at 21:35
  • "Does a declared function have/produce a value?" - Is this not a return value question? – evolutionxbox Sep 16 '16 at 21:38
  • @evolutionxbox not if you read that sentence as "Does _declaring a function_ have/produce a value" as opposed to "Does _executing a function_ have/produce value". All the examples in the OP are with declarations and sometimes assigning those to a variable, i.e., using the values produced by the declaration itself. At no point was there any indication that the OP either meant or even care about _executing_ the function. Aside from the very first example given which was there merely to show _syntax_ not to be used as the basis of the entire question. – VLAZ Sep 16 '16 at 21:50