FAQ: Functions - Function Declarations


#1

This community-built FAQ covers the “Function Declarations” exercise from the lesson “Functions”.

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

Web Development

Introduction To JavaScript

FAQs on the exercise Function Declarations

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

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!


#2

Hello there! I am getting a little confused as to when semicolon ; should be used after curly braces {}. I see most times it is not used, but sometimes it is. Could you please clarify this? Thanks!


#3

You’d look at what the statement in question looks like, either it includes a semi-colon, or not.
Redundant semi-colons results in empty statements which is why you won’t notice having too many, and when they’re missing javascript adds them anyway which is why you also won’t notice if there are too few.

Curly braces have different meanings depending on what they’re part of. They’re not their own “thing”.


#4

For instance, in a function like this, do we need semicolon after the curly braces at all?

const example = () => { if (...) { ... } }


#5

That has nothing to do with curly braces. Look up the statements to find out if they include semicolons.

I count 3 statements there.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/block
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/const
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/if...else


#6

There seem to be multiple formats for writing the code in this lesson, which causes confusion. Page 2 of 11 dictates that calling a function requires console.log(identifier());. Page 3 of 11 states only identifier(); is needed. Requesting the solution is the first time identifier(); (without console.log) is introduced as a way to call the function.

To be more clear, this is on page 2:
function greetWorld() {
console.log(‘Hello, World!’);
}
console.log(greetWorld()); // Output: Hello, World!

Page 3 requests a function to be called 3 times. Here, “console.log(identifier());” is wrong. This is the method the program now wants:
functionName();


#7

When our function does the logging, we won’t be logging the return value, which will be undefined.

When a function returns a value, we can log that return…

function greetWorld() {
    return "Hello World!";
}
console.log(greetWorld())

#8

In what case do you use console.log(greetWorld();? What is the appropriate use for this command? Could you provide a practical example of how it is different from greetWorld();? The language that you used is not making sense to me.


#9

It had a typo that’s fixed now.

When the function is not logging, we would log the return value at the caller.


#10

Thank you for responding, but this verbiage doesn’t make it clearer for me. It would be more useful to have practical examples.

The function execution diagram image on page 3 of 11 does not display. Maybe it would be clearer for me if the image src wasn’t broken.


#11

I’m also struggling to wrap my head around this a bit - the undefined part…

I understand calling the function, as per the next exercise, like so:

function getReminder() {
  console.log('Water the plants.');
}
getReminder();

Logged:

Water the plants.

The function is declared, and then the “instructions” in the body are called using the identifier.

…and I understand returning a value by printing to console, as @mtf described above:

function getReminder() {
  return "Water the plants.";
}
console.log(getReminder());

Logged:

Water the plants.

We declare the function and then print the result of the instructions “return a value”.

…but the below exercise, where you do the following does not make sense.

function getReminder() {
  console.log('Water the plants.');
}
console.log(getReminder());

Logged:

Water the plants.
undefined

So the function is declared first (which on its own would not log anything), then console log which includes the function identifier… is this the same as calling the function and why we get “Water the plants” printed? Why is undefined printed? Is the second console.log seen as empty even though it contains the identifier?

Thanks in advance :slight_smile:


#12

Hey, I think this is the image your looking for:


#13

Thank you very much :slight_smile: