Where is Function Declaration's returned value stored?


#1

Hello everyone,
I have a ‘theoretical question’:

Where is Function Declaration’s returned value stored?

I’ m asking this, because in the case of Function Expressions, or Arrow Functions, the returned value is stored in a variable declared as ‘const’. So, what about the Function Declaration? Where is its returned value stored?

Thnx in advance


#2

That’s a statement, it doesn’t evaluate into anything

However, it does create a variable and makes that name refer to the function. The name is part of the statement.

Invalid syntax, statements have no result, can’t use its resulting value, there isn’t one

const f = (function f() {
})

Oh. It’s actually valid syntax. What.


#3

if i look at the documentation (link), we see this:

function square(number) {
  return number * number;
}

so the function is stored in a variable named square. This name you can usually choice

const is a reserved keyword, it only indicates that the variable is a constant, its not the name of the variable


#4

Thanks for the answers

I think I got it

In either case, the returned value is stored in a variable.
The only difference is that, in the case of Function Declaration, we declare this variable as ‘const’.

Have I got it right?


#5

No, there’s no result at all. It’s not an expression

1 + 1 has a result
this does not:

if (true) {}

It has the side-effect of creating a function


#6

no, you need to understand what const is and what it does. Maybe check some documentation?


#7

Right, my example from before:

const f = (function f() {
})

Was actually a function expression (so there was a result which could be assigned to f). THIS though,

function f() {
}

Is a statement (and therefore has no result, it does something

And const itself has nothing to do with functions, it’s for creating un-changeable variables:

const n = 5;

But you can use it to create a variable which refers to a function


#8

Thanks again

I really appreciate the help

So, I need to read the documentation, in order to better understand functions (…or any other thing related to javascript) -and I will.

But… until then… let’s see if I got this right

A function expression has a result which can be assigned to a variable (in our case, f)
While, a function, is just a statement. It has no result. It just does something.

But, let’s take a concrete example

function square (num) {
	return num * num;
}

console.log(square (5));

In this case -according to what ionatan said- we have a statement
Now, the ‘thing’ that this statement ‘does’ (return num * num), is stored nowhere? Is this right?

So, when I call this function (square (5)) inside of a console.log (console.log(square(5))), what is it that happens?
The result of num * num is calculated, returned and printed (through console.log())? But this calculated result is stored nowhere?
So, the result is only calculated anytime we call the function, but never stored?

P.S.: Sorry for all these many questions! I am a newbie, but I feel that in order to get good at javascript, I need to first understand it…


#9

Similar to how 1+1 has a result of 2, you get a function, yes.

Well, no, a function is a value which you can call. You can create them with function statements, function expressions, arrow functions (which are also expressions), and with the Function constructor

The thing it does is to create a function and assign a name to it.

That’s the content of the function, the function statement does not do that, that happens when the function is called. That code (in some internal format, not text) is stored in the function object.

That depends on which result you mean. There’s the result of a function expression, and there’s the result of the function call (which is an expression)

Also, there’s nothing stopping functions from creating functions

(function() { return function() { return 5 } })()() // 5
// (create a function. call it. the result is a function.
// call *that*, its result is 5)

#10

Thanks again for your time my friend

I think I need to study all these -and practise


#11

I think it just comes down to the realization that functions are values. You can create them, and you can call them. You can pass them around in your program, store them in arrays… They’re values. There’s not all that much more to javascript than functions and objects, bolted together in every imaginable way.


#12

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.