Basic JavaScript - Understanding Undefined Value returned from a Function

Tell us what’s happening:
Describe your issue in detail here.

Your code so far

// Setup
let sum = 0;

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

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

// Only change code above this line

var result = addFive();

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/ Safari/537.36

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

Link to the challenge:

“Create a function addFive without any arguments*.”

Your code:

function addFive(sum){ ... }

You are passing an argument into the function.

Small correction. They have added a parameter, they are not passing an argument.

This leads to an important point. Parameters are variables scoped to the function. So sum inside the function and sum outside the function are not the same identifier.

When you create a parameter named the same as an outer variable you are shadowing the variable. You now have two variables named the same in different scopes. Assigning a value to sum inside the function will not assign it to sum outside the function.

Parameters are undefined by default and as no argument is passed undefined + 3 is NaN.

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