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.

Notice how we assign announceThatIAmDoingImportantWork without parentheses as the value to the busy variable. 
We want to assign the value of the function itself, not the value it returns when invoked.

i didn’t understand what this is trying to say, can someone explain please ?

we pass the function by reference, which mean we can do the actual calling/invoking of the function later.

Hi! Could someone tell me why would we write in the code below; if ( (2 + 2) =! 4 ) instead of if ( 2 + 2 != 4 ) please?

// checkThatTwoPlusTwoEqualsFourAMillionTimes. This function takes a long time to execute. It checks whether 2 + 2 = 4, but it does it a million times (just to be really sure)!

const checkThatTwoPlusTwoEqualsFourAMillionTimes = () => {
  for(let i = 1; i <= 1000000; i++) {
    if ( (2 + 2) != 4) {
      console.log('Something has gone very wrong :( ');
    }
  }
}

// Write your code below
const is2p2 = checkThatTwoPlusTwoEqualsFourAMillionTimes;

is2p2();

like the function name says:

checkThatTwoPlusTwoEqualsFourAMillionTimes

we want to check that two plus two does not equal 4, and we want to be really sure, so we do this a million times

= is assignment operator, != and == are comparison operators.

1 Like

My mistake! I meant why we added the parentheses inside the if condition’s parentheses like this: if ( (2 + 2) =! 4 ) instead of simply write it like this: if ( 2 + 2 != 4 ) ?

you mean != ?

the parentheses here are not needed indeed.

1 Like

Yes. i made another mistake! Really apologize :pray:

So why they showed us this example in the exercise if they are not needed?

some people prefer to be explicit and not rely on order of operations? Why people do certain things is sometimes a mystery.

1 Like

Ok i get what you’re trying to say!

Thank you for explaining this :pray:

If I create second “copy” of first function and then change first function content, second “renamed” function stays intact?
Example:

function multip01(x) { return x * 2 } // first function declaration
multip02 = multip01 // second function referencing to first function
function multip01(x) { return x * 3 } // first function new content declaration

console.log(multip01(5)) // this will produce 15 as 5 * 3 = 15
console.log(multip02(5)) // this will produce 10 as 5 * 2 = 15
console.log(multip02.name) // this will produce function01 like nothing ever changed

I think you have to distinguish between assigning a REFERNECE to the function versus assigning the VALUE returned from the function
const shortFunc = thisIsAVeryLongFunctionName; // assigns REFERENCE that points to thisIsAVeryLongFunctionName

both of these lines assign the VALUE returned from the function
const theValue = shortFunc();
const theValue = thisIsAVeryLongFunctionName();

thus, the following line of code would assign the VALUE returned from the function rather than the REFERNECE pointer to the function
const shortFunc = thisIsAVeryLongFunctionName();

I have question of using the name property: How “deep” does the name property look? Will it go back all the way to the root?

Let me explain: Assuming I have nested a function within a function within a function like so:

const nestingLevel3 = () => {
   console.log('I am the root function!');
};

const nestingLevel2 = constNestingLevel3
const nestingLevel1 = constNestingLevel2

Would

console.log(nestingLevel1.name)

return nestingLevel3 ???

Easy enough to test, right?