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

Micah Dunson
Micah Dunson
34,368 Points

How can I alter the 'elevatorCloseButton' function to follow the best practices?

I'm struggling learning this JavaScript. I don't understand what they mean by this question. It either tells me to assign a "status" to the 'IF', or I need to include status within the function. I need help!!!

2 Answers

Hugo Paz
Hugo Paz
15,622 Points

You need to declare it at the start of the function.

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

    }

From https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/var

"Because variable declarations (and declarations in general) are processed before any code is executed, declaring a variable anywhere in the code is equivalent to declaring it at the top.

This also means that a variable can appear to be used before it's declared. This behavior is called "hoisting", as it appears that the variable declaration is moved to the top of the function or global code."

Micah Dunson
Micah Dunson
34,368 Points

Thank you! It totally makes sense and seems blatantly obvious now lol. I was just not seeing it. Thank you for the help and the link to the reference. Much appreciated.