FAQ: Functions - Function Declarations

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!

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!

2 Likes

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”.

1 Like

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

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

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

2 Likes

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();

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())
1 Like

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.

It had a typo that’s fixed now.

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

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.

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:

6 Likes

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

6 Likes

Thank you very much :slight_smile:

what is necessity of returning a value to calling function??

To get rid of the “undefined” being printed-

simply type the following:


getReminder();
greetInSpanish();

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

function greetInSpanish() {
console.log(“Buenas Tardes.”);
}


Console logging the function name is what causes it

you never need it, semicolons are optional, but if you’re going to use them, use them consistently. Semicolons are used to mark the end of the line to help others read your code. since we use multiple lines to display one line of code, it makes it easier to read. so your example:

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

would have the semicolon at the end of the line, but if we wrote the line out like this:

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

the two are the exact same just divided up on different lines to help us read it but the semicolon should be at the end of the line of code.

You also could end the if statement with a semicolen before the curly braces “{}”:

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

some times, theres a line of code, winin a line of code. like with a do…while statement:

do {
    if (...){
    };
    if (...) {
    };
    var++;
    if (...) {
    };
while (...) {
};

we use the semicolons to divide the different codes we want to run within the do statement

2 Likes

There are no indications in this first lesson on functions that I needed to provide a function call in order to have anything log to the console. I had to do a google search to find that out.
Instead of being clever and talking about hoisting, you might want to work on clearer instructions.

1 Like

What about the example in the narrative?

console.log(greetWorld()); // Output: Hello, World!
//          function call
function greetWorld() {
  console.log('Hello, World!');
}

JavaScript Hosting.
Hello, I’m hoping you can answer my question about JavaScript Hosting.
When would you use it? Is there any situation when it useful to declare a variable before its used, or is it just a quirk of JavaScript to be aware of? I’ve had a nosy round the Internet, plenty of articles about how hosting works but nothing on when/if you would potentially use it. Cheers

1 Like

I believe the term you are referring to is, ‘hoisting’. Give that a go around and see what you find. Start with MDN, and go from there.