- Basic JavaScript - Understanding Why Undefined Value is returned from a Function

Tell us what’s happening:
I’m able to complete the step. My question is why is the value undefined? When I think about it, logically, the value should be defined. So I’m probably not thinking about the guts of JavaScript correctly. What I’m looking for is the reason this is undefined so that I can understand JavaScript logic.

Your code so far

// Setup
let sum = 0;

function addThree() {
  sum = sum + 3;
}

// Only change code below this line
function addFive() {
  sum = sum + 5;
}

// Only change code above this line

addThree();
addFive();

Your browser information:

Challenge: Basic JavaScript - Understanding Undefined Value returned from a Function

Link to the challenge:

The function doesn’t return a value from the code inside because there is no return statement. So, whilst the code may increment the value of sum within the function, that value is not returned by the function. This would be fixed easily however by simply changing ‘sum =’ to ‘return’. Then, when you call the function the returned value will be whatever follows the return keyword.

Thanks Igor, That clarifies the syntax for me.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.