FAQ: Higher-Order Functions - Functions as Data

This community-built FAQ covers the “Functions as Data” exercise from the lesson “Higher-Order Functions”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Web Development

Introduction To JavaScript

FAQs on the exercise Functions as Data

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

Not really a question (if not allowed pls delete) but did anyone else find this exercise as hilarious as I did?

3 Likes

i don’t see why this is not allowed, but now you made me curios. Why did you find this hilarious?

its more of the console log that i found hilarious for some reason, I think its just imagining the computer finding that 2+2 not equaling 4 after a million times of checking funny. (edit: and the frowny face XD)

6 Likes

why property ‘name’ returns assigned function?

1 Like

because its a built-in property of the JS language:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/name

I’m having trouble understanding the benefits of declaring a function as a variable as described in the lesson. In the example for this course, the variable busy was declared to be a shorter name for announceThatIAmDoingImportantWork() so then it can be called with busy() instead. In what case scenario(s) would it be advantageous to do this? Wouldn’t it just be simpler to declare a function with the simple name instead of having a complex name and renaming it later on?

2 Likes

So many aspects to this question and its answer.

A function should always have a descriptive name, so that you know what the function (roughly) does without having to through the code (a comment/type hint with the returned possible data type(s) then also helps). Preferable the name should not be too long, but should be descriptive. always a tricky balance.

we are not renaming the function. We declare another variable which points to the same function in memory.

3 Likes

Hi! can anyone tell me why my codes not working?

sneaky upper cased l

In this exercise we reassign a constant variable (const checkThatTwoPlusTwoEqualsFourAMillionTimes)
How is that possible?

its not possible, can I see your full code please to understood what you did?

Sorry, here it is:

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

const is2p2 = checkThatTwoPlusTwoEqualsFourAMillionTimes;
is2p2();
console.log(is2p2.name);

The function checkThatTwoPlusTwoEqualsFourAMillionTimes is stored in a constant variable yet we are able to reassign it.

You mean this line:

const is2p2 = checkThatTwoPlusTwoEqualsFourAMillionTimes;

We do not re-assign the checkThatTwoPlusTwoEqualsFourAMillionTimes variable here. We simple make a new variable (is2p2).

re-assigning would be to do:

checkThatTwoPlusTwoEqualsFourAMillionTimes = 'some new value';

which is not allowed, because the variable is constant.

3 Likes

Oh, all right, thanks a lot, I think I get it now. :slight_smile:

What is the difference between calling and invoking a function?

1 Like

Nothing as far as I know, just different terminology used to describe the same things.

2 Likes

This lesson states that when we assign a function to a variable like so:

const thisIsAnUnnecessarlyLongName = () => {
  console.log("Hello, World!");
}

const shortName = thisIsAnUnnecessarlyLongName;

The shortName variable holds a reference to the thisIsAnUnnecessarlyLongName function. So, if we were to log the place in the memory where shortName and thisIsAnUnnecessarlyLongName are being held, they would point to the same place

I don’t really understand what it means when we say that it holds a reference to the thisIsAnUnnecessarlyLongName function

I also thought that if we changed the value of thisIsAnUnnecessarlyLongName, shortName's value would also be altered

now we can do:

shortName()

which will execute the function. Both variables point to the same memory/memory address.

Not sure how many times I have done this analogy, but it helps understanding so here it goes:

Imaging the house you live in. The house (function) has an address. You can hand out/give the addresses to two (or more) more friends (variables), but there is still only one house

hopes this helps

2 Likes

ok, so when you invoke shortHand you are actually invoking iHaveAnUnnecessarlyLongName, and when you try to access shortHand (like when you try to log shortHand.name), you are actually accessing iHaveAnUnnecessarlyLongName?