FAQ: Higher-Order Functions - Functions as Data

Too truly grasp this concepts, an understanding of pointers will help. You could look at C (programming language) course which explains pointers? That could help

Not the way I would put/say it. The function is in memory (RAM -> random access memory, this is where a program lives while running). Both these variables point to/reference the function in memory.

Given both these variables point/reference the same function, trying to invoke either will call the same function in memory

1 Like

And when you do console.log(shortName.name);, it will actually log iHaveAnUnnecessarlyLongName to the console. How do you explain this?

And could you say that shortName is a function?

I was not familiar with that concept, I checked the documentation (link), which says:

A Function object’s read-only name property indicates the function’s name as specified when it was created, or it may be either anonymous or ‘’ (an empty string) for functions created anonymously.

If I read that correctly, JS seems to set a property with the original name. Doesn’t change anything about my explanation as far as I can see? Both variables still point to the same function in memory which has a property with the original name as well

1 Like

Ok, thanks so much! I think I understand it well

One last question, could we say that shortName is a function? And, when we say const shortHand = iHaveAnUnnecessarlyLongName, we are just assigning shortHand the function in the memory that iHaveAnUnnecessarlyLongName points to?

I suppose we could, given:

console.log(typeof shortName);

gives us: function

But variables are just really labels in a sense. I have never been particular per-occupied with the exact technical naming. More interesting in understanding concepts and applying them

mtf or ionatan are undoubtedly better equipped to answer this kind of question :wink:

1 Like

What is the difference between defining a function like “function” and defining it as “const”?

I have tried to define a function and then assign it to a const but that doesn’t work:
function test(){ console.log(‘Hello’) }
const test2 = test;

So, when should we use “function” and when should we use “const” to define a function?

function test(){ console.log(‘Hello’) } is “old” es5 syntax.

Preferable I would always use es6 when possible:

const test = () => {
   console.log('Hello');
}

this prevents you from accidentally overwriting the function.