Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript JavaScript Foundations Variables Hoisting

Nazaam Kutisha
Nazaam Kutisha
7,667 Points

Help with this challenge

Not clear on what is happening. I've defined the variable with global scope, but Im still not clearing the challenge. What am I missing???

My code response was

<script> var status function elevatorCloseButton(pushed) {

    if (pushed) {
        status= "I'll close when I'm ready.";
  consol.log(status);

    }

}

elevatorCloseButton(true);

</script>
Hayden Taylor
Hayden Taylor
5,076 Points

Could you post some code, the question and the task at hand please. Although it says hoisting ... it doesn't link anywhere.

Hayden Taylor
Hayden Taylor
5,076 Points

I know where the link is haha but it didn't work for me is what I was saying but the answer below outlines everything you need to know

3 Answers

Hi Nazaam,

By "global scope" I'm assuming you declared the variable outside the function?

The instructions state that you should declare the variable within the scope of the function but follow best practices in doing so.

You start off with:

function elevatorCloseButton(pushed) {

    if (pushed) {
        var status = "I'll close when I'm ready.";
    }
}

Javascript doesn't have block level scope, it has function scope instead. So just because the variable is declared inside the if block that does not mean it's only available inside the if block. It is still available outside the if block but still within the function.

What javascript does is hoist that variable declaration to the top of the function.

Here the instructions are asking you to make that explicit.

function elevatorCloseButton(pushed) {
    var status; 
    if (pushed) {
        status = "I'll close when I'm ready.";
    }
}

So here we're making it clear the way javascript would treat it. The variable is declared within the scope of the function and then the assignment statement can happen inside the if block.

Nazaam Kutisha
Nazaam Kutisha
7,667 Points

There is a link to the code challenge under related content . My code response was:

<script>
        var status 
    function elevatorCloseButton(pushed) {

        if (pushed) {
            status= "I'll close when I'm ready.";
      consol.log(status);

        }

    }

    elevatorCloseButton(true);

    </script>

You were close and had the right idea but just went too far up with the variable declaration.

Nazaam Kutisha
Nazaam Kutisha
7,667 Points

Whats up Jason, Brillant reply. This syntax passed the challenge. Thanks for the prompt response bro.

You are welcome.