Understanding Undefined Value returned from a Function 1

Tell us what’s happening:
so the main idea of this challenge is that a change in the sum of a variable in the global scope could be because of a missing return statement in a local scope function, which in return causes the function to be undefined?

Your code so far

// Example
var sum = 0;
function addThree() {
  sum = sum + 3;

// Only change code below this line

// Only change code above this line
var returnedValue = addFive();

Your browser information:

User Agent is: Mozilla/5.0 (X11; CrOS x86_64 11021.81.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.110 Safari/537.36.

Link to the challenge:

so far so good… you now need to replicate the example with this case:

Create a function addFive without any arguments. This function adds 5 to the sum variable, but its returned value is undefined.

No, the point of this one is that, while functions CAN return a value, they are not REQUIRED to. Functions do have access to global variables, and in this case, that’s the only way to see that the function has done something.

So you don’t want to return something in your addFive(), you simply want to affect the global variable sum.

1 Like

Sort of… what @snowmonkey said is key… they give an example where the sum changes… but a ‘returned’ value was undefined since it returned no value, but did change the value of sum

but it’s not because of the missing return that the global scope changed, that happened anyway

If you had a console.log(sum); in there you would see sum would be 3

but if you did something like var returnThree = addThree(); and did a console.log(returnThree); you would get undefined…

savvy that?

1 Like

So is this challenge is revealing that I have “free will” with return statements. but with that I will just have a undefined function.

1 Like

if i added a return statement i would have a value right.

Could do. You have complete free will. You can return true, or return sum, or return 'THIS_IS_A_STRING' or anything you like.

As you move forward, it is really useful to use those return values. Often you’ll see something like
var mySum = addThree(12);

which would assign that returned value to mySum.

When you get further down the way, you’ll start referring to the Mozilla Developer Network, MDN, like a LOT. And one of the most important things you’ll find there, at least in my view, is the return values. Some functions return values, some don’t. Some return values that don’t seem logical at first glance (for example, the Array.splice() returns deleted elements, even if you haven’t deleted anything, or Array.push() returns the new length of the array).

You can return, you don’t have to return, but you’ll find sometimes return is invaluable. Best of luck.


yes. I’m sure there are cases where no return value is needed… but if you want your function to do something, you often return the value of that whatever you wanted it to do

Gotcha. Appreciate the help!

1 Like