Global/local space in functions

Two questions re: the following code:


var answer
function sequentialSizes(val) {
 answer= "";
switch (val) {
  case 1:
  case 2:
  case 3:
  answer = "Low";
  break;
  case 4:
  case 5:
  case 6:
  answer = "Mid";
  break;
  case 7:
  case 8: 
  case 9:
  answer = "High";
  break;
}
 // Only change code above this line
 return answer;
}

sequentialSizes(1);
console.log(answer) //prints Low

var outerWear
function myOutfit() {
outerWear = "sweater" 
return outerWear;
}
myOutfit();
console.log(outerWear) //prints sweater

First, in either example, if I don’t call the function by removing the lines

sequentialSizes(1)

or

myOutfit()

undefined is printed to console for the respective variable, answer or outerWear.

Why is that? I would have thought that the variables were declared globally within the functions in these examples, in the absence of var, such that the function wouldn’t have to be called subsequently for the variables to be re-assigned from within the function?

Second question: if I re-write any of the lines within the switch statements from the first example from

answer ="Low" // or "Mid" or "High"
to
var answer="Low" // or "Mid" or "High"

the variable answer reads undefined in the console- it seems the scope of answer within the switch statement is only global if it is global in every instance? but if it is assigned with var in any instance, it is local for the entire switch statement? Is that right? Is that an aspect of switch statements specifically?

Thanks for any insight!

   **Your browser information:**

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:86.0) Gecko/20100101 Firefox/86.0.

Challenge: Multiple Identical Options in Switch Statements

Link to the challenge:

You should not write functions to modify global variables like that. You end up with code that is fragile and hard to reason about.

In this case, answer and outerWear are undefined until the functions run because you do not set an initial value for the variables. When you do not set an initial value, the variable is undefined. The functions don’t run if you don’t call them, so the variables remain undefined.

With switch statements, if you use the var keyword, that means that you are defining a local variable answer instead of modifying the global variable. This isn’t something special about switches. Whenever you declare a variable inside of a function, via var, let, or const, that variable is only in scope inside of the function.

Again though, you shouldn’t write functions that directly change global variables. You should instead update the values in those variables based on the function return value.

var myVariable;
myVariable = sequentialSizes(4);

Thanks- upon reflection I can see how trying to understand why something problematic (ie defining variables globally from within a function) doesn’t work as I expected in this circumstance is rather unproductive- it shouldn’t be done in the first place for obvious reasons.

Re: switch statements, I was just surprised that if I use the var keyword for only one out of multiple cases, and omitted it in the others, it changed the result as though I had used it for all of them. That said, I see how this is the same issue: this shouldn’t come up, because it would be self-defeating to write code that relied on functions that directly changed global variables.

I think I lost the forest for the trees a bit.

var can be sort of funky like that. The behavior is more like what you’d expect with const or let