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

Michael Criste
Michael Criste
5,045 Points

I don't know what I'm missing here, so it's difficult to craft a specific inquiry. Please help.

Please see attached code. I know this is a simple concept: hoisting. I know this means having a variable defined outside the function so that it takes precedence, but I'm missing the syntax somehow, perhaps with how it's written within the function. I've tried several ways, but without success.

index.html
<!DOCTYPE html>
<html lang="en">
  <head>
    <title> JavaScript Foundations: Variables</title>
    <style>
      html {
        background: #FAFAFA;
        font-family: sans-serif;
      }
    </style>
  </head>
  <body>
    <h1>JavaScript Foundations</h1>
    <h2>Variables: Hoisting</h2>

    <script>

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

    }

    elevatorCloseButton(true);

    </script>
  </body>
</html>

2 Answers

Hi Michael,

You're almost there with that one. Try this:

<script>

    function elevatorCloseButton(pushed) {

        var status;

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

    }

    elevatorCloseButton(true);

</script>

-Rich

Michael Criste
Michael Criste
5,045 Points

Interesting. So within the function you're ensuring that the variable is present, but without setting it equal to something it becomes malleable, to be redefined with each branch in branching and/or conditional statements.