Basic JavaScript - Global vs. Local Scope in Functions

Tell us what’s happening:
Describe your issue in detail here.
Good morning to all my bosses and friends.
I did this already and the code passed but I have a problem with how this works.
Take a look at the code below as used in the previous example. The function myFun(); should return “Head” but it’s returning nothing after a series of test.
const someVar = “Hat”;
function myFun() {
const someVar = “Head”;
return someVar;
}
myFun();
This is why I’m worried.
It returns nothing when I tested the code with Js Run

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();

Your browser information:

User Agent is: Mozilla/5.0 (Linux; Android 10; K) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/115.0.0.0 Mobile Safari/537.36

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

Link to the challenge:

Hi @vincowealthy,

The function you provided is a return function that only returns a value without printing it out to the console.

Instead of:

myOutfit();

You can use:

console.log(myOutfit());

Using console.log() will help you greatly, especially when it comes to solving bugs.

Thank you very much.
Not only am I appreciative but I’m also proud of your kind care.
God bless you

1 Like

The code is returning “Hat” instead of “Head”

Please show me the code.

const someVar = “Hat”;
function myFun() {
const someVar = “Head”;
return someVar;
}
myFun();

I tried it, it will return head.

image

What about console.log(someVar);
?

This is completely different.

const outerWear = "T-Shirt";
function myOutfit() {
const outerWear = "sweater";
  return outerWear;
}

myOutfit();

In the previous code, there are two variables that share the same name but have different roles. One is global in scope, and the other is local.
run this code so you can understand the differences:

const outerWear = "T-Shirt";

function myOutfit() {
const outerWear = "sweater";
  console.log(outerWear);
}

myOutfit();//output "sweater" -> local varaible
console.log(outerWear);// gloable varaible

I found there’s no return keyword in the code.
I really appreciate your time, care and effort

You can add return, but I replaced it with console.log() statement to understand it more.

You can use this :point_down:

const outerWear = "T-Shirt";

function myOutfit() {
const outerWear = "sweater";
  return outerWear;
}

Console.log(myOutfit());//output "sweater" -> local varaible
console.log(outerWear);// gloable varaible
1 Like

Thank you very much for your unalloyed support

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