Basic JavaScript - Assignment with a Returned Value

Tell us what’s happening:
Im confused why this works since the line
processed = processArg(7);
is “out” of the function. Seems like you wouldnt be able to just declare processed can equal anything without using a keyword in front of it, far less declaring its value outside of the function when it was declared globally at the top of the function. im so confused. :sob:

Your code so far

// Setup
let processed = 0;

function processArg(num) {
  return (num + 3) / 5;

// Only change code below this line

processed = processArg(7);

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/ Safari/537.36

Challenge: Basic JavaScript - Assignment with a Returned Value

Link to the challenge:

processed is being declared with the let keyword. That is being defined globally. You are overwriting the value of processed with the return value of processArg(7). Or in other words, you are reassigning the value of processed. If it were defined inside of the function, the variable would be local to that function ie anything inside of the function body { and } is local and only accessible to that function. That would be a local scope as opposed to a global scope. const variables cannot be reassigned.

  • this works cause its been declared in global scope let processed = 0;
  • if you would have “re-declare it with a keyword (i.e. var, let, const) infornt of it again in global scope” then it would have been a problem in global scope
  • this simply getting return value from “processArg(7)” and then re-assigning it to “processed” variable

hope this was helpful, happy learning :slight_smile:

I think I understand what you are saying here-
so, because it was defined in global scope, you can redefine it again globally (under the function and out of the curly brackets) by writing
processed = the return value of the function?
and thanks for the help!

i think im starting to understand… thank you.

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