Where does the "return" value go to?

Tell us what’s happening:

Your code so far


function caseInSwitch(val) {
  var answer = "";
  // Only change code below this line
  switch(val) {
  case 1:
  return "alpha";
  break;
  case 2:
  return "beta";
  break;
  case 3:
  return "gamma";
  break;
  case 4:
  return "delta";
  break;
  }
  
  // Only change code above this line  
  return answer;  
}

// Change this value to test
caseInSwitch(1);

Hello, I’ve completed the task but I still have a question:
whenever the javascript function is called or executed and some of conditions in function is met, one specific “return” command is executed, the returned value goes wherever, the log for example…But there is a variable “answer” is the to value “”"".

image .

does this mean that the value that “return” sends back when condition of the switch statement gets returned goes into the value of the variable “answer” ? because in the end of the function it is set to return the “answer”:

image .

Typically, you want to handle that return value. It can be that it gets stored into a variable, or acted upon in some other way. For example, a better way to call your caseInSwitch(1) might be:

let myWord = caseInSwitch(1);

That would store the returned value into a variable, which we could then use elsewhere. Another possible way it might be used is as an “instant value”:

if(caseInSwitch(2) === "beta"){ ... }

In that case, we take the value returned from your function, and use it immediately. We don’t need to store it, as we’re simply using it for a comparison.

But yes, if a value is returned, it is usually a good plan to do something with it.

Thanks, thats useful to know.
And about this specific code that i posted, case statements will return value into the answer variable ? If inside some function you declare a var before the case or if statements, will those statements return their values into the var declared before them ?

Whats happening is, your function is passing the argument val. First thing your function does is set variable of answer to an empty string, then it pops into your switch statement to check if any of the case statements match the value of val. If it finds one, the function will move on to the next instruction, which is to return a string (“alpha” or “beta” or etc)

If it runs through your entire case statement and doesn’t find a match, then it will come to the end where it sees its instructed to return answer which is an empty string. When you run the function with a value of 1-4, it will return the value set for that number. If you run the function with a value of 5, for example, it will return the empty string.

Two things:
Every function needs to return something - this is why it returns answer, which is a blank string, if it finds no matches in the case statement.

The moment you return something, the function immediately stops and exits with whatever its been told to return - Which is why the last return of answer does not do anything if it does find a match in the case statement.

1 Like

Huh. @cndragn, you just taught me something. My kneejerk reaction was that I was about to argue with you about “Every function needs to return something” – I’ve always thought that, while it is good practice to return something, they don’t need to. Turns out, I’m a bonehead.

I wandered over to the MDN (Mozilla’s web docs), and learned straight from the horse’s mouth:

To return a value other than the default, a function must have a return statement that specifies the value to return. A function without a return statement will return a default value. In the case of a constructor called with the new keyword, the default value is the value of its this parameter. For all other functions, the default return value is undefined .

In other words, you don’t NEED to define a return value, but your function will ALWAYS return something, even if it’s undefined.

1 Like

Hahaha yup, I learned this one with lots of trial and error… a function will always return something, whether you tell it to or not, but if you dont tell it what to return, then that something will break all the things!

1 Like