Basic JavaScript - Global vs. Local Scope in Functions

Tell us what’s happening:
Describe your issue in detail here.
myOutfit should return the string sweater .

My code does not show “sweater” in console.
It shows t-shirt, and it also lets me pass the challenge even though it shows the wrong string. How can I fix this?

Your code so far

// Setup
const outerWear = "T-Shirt";

function myOutfit() {
  // Only change code below this line
  const outerWear = "sweater";
  // Only change code above this line
  return outerWear;
}

myOutfit();
console.log(outerWear);

Your browser information:

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

Challenge: Basic JavaScript - Global vs. Local Scope in Functions

Link to the challenge:

This function call doesn’t make changes outside of its scope (which is a good thing!)

so it’s correct? even though console.log shows t-shirt? I thought it would show the local variable and not the global.

Because it says like this “When you do this, the local variable takes precedence over the global variable.”

You aren’t logging what the function is doing

I’m very new at javascript so sorry if i’m abit slow to understand, but is it possible to log a function in console? if I use console.log(myOutfit); for example it’ll only show “[Function: myOutfit]”

To clarify so you understand, I wanna console.log this out so it returns “sweater”. Or am I not supposed to do that in this challenge? Because it says that the local variable should have precedence over the global variable, but it shows the global variable when i try to console.log it

You can log out the return value of the function. You would need to put the function call inside of the console.log

2 Likes

Right, thanks.
can you explain why;
console.log(myOutfit); Does not work? but
console.log(myOutfit()); does work?

I cant seem to figure out why the extra parentheses is needed. you dont have to explain but i would appreciate it so I understand :smiley:

Well, what did previous lessons say happens when you add the ()?

myOutfit is the function while myOutfit() makes the function run

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