Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Function expressions vs function declarations: return value

Tags:

javascript

In a Udacity lesson the difference between function expressions and declarations is explained as follows:

A function declaration defines a function and does not require a variable to be assigned to it. It simply declares a function, and doesn't itself return a value ... On the other hand, a function expression does return a value.

This is confusing; to my knowledge, when both function expressions and function declarations include a return statement, both return a value.

If I understand correctly, the difference with respect to return value is that in a function expression, if a value is changed in the first call of the function, in a subsequent call the updated value would be preserved—whereas if the function were not stored in a variable, the return value would be erased when the function is finished executing. Am I missing something, and is the statement from the lesson accurate?

Note: My question is different from the one marked as a duplicate. In that question it asks what the reasons for using one over the other, but return value is not mentioned in the question or explained in its answers.

like image 475
nCardot Avatar asked Jun 16 '18 20:06

nCardot


2 Answers

To understand what this really is about, we need to dig into the JavaScript grammar:

In ECMAScript a script consists of Statements and Declarations. A Statement can be (amongst others) an ExpressionStatement. Note that ExpressionStatement is explicitly defined as:

ExpressionStatement[Yield, Await]:

[lookahead ∉ { {, function, async [no LineTerminator here] function, class, let [ }]

Expression[+In, ?Yield, ?Await];

This looks really cumbersome, but what it says is that an ExpressionStatement cannot possibly start with the keyword function. So if you just write:

function a() {}

This can never be interpreted as an expression although in other circumstances like

const a = function a() {}

it is an expression. (The right hand side of an assignment operation always must be an expression.)

Now, only expressions evaluate a value, statements do not. This is all the text you quote is saying in a hard to understand way.

A function declaration defines a function and does not require a variable to be assigned to it:

True but redundant. A declaration cannot occur at the right hand-side of an assignment.

It simply declares a function, and doesn't itself return a value ...

Yeah, statements do not evaluate to ("return") a value.

On the other hand, a function expression does return a value.

Sure, like any expression.


See https://www.ecma-international.org/ecma-262/8.0/#prod-StatementList

like image 198
idmean Avatar answered Sep 30 '22 04:09

idmean


The definition isn't talking about the function returning a value, it is talking on how one way of creating a function returns a value (which is the function expression) and another just declares a function (a function declaration).

To help you clarify things, you should understand first what an expression is:

An expression is any valid unit of code that resolves to a value.

One example of an expression would be x = 5, which evaluates to 5. Another example would be 3 + 2, which also evaluates to 5.

In other words, we could say that both expressions above return a value of 5.

A function expression is just an expression that returns (evaluates to) a function:

// expression
const whatever = function expression() {}
//               ^^^^^^^^^^^^^^^^^^^^^^^^ this is the function expression in an assignment statement

A function declaration is not an expression but a statement. It doesn't need to evaluate to a value. It is just immediately declared:

// declaration
function example() {}

How a function is created (via a declaration or an expression) doesn't affect what the function can return - that capability is the same in both cases.

like image 27
nem035 Avatar answered Sep 30 '22 05:09

nem035