2

I'm surprised that the following code works:

while(fred !== "stop"){
    var fred = prompt("Should I stop or go?")
};

I'd understand this in a do while loop:

do {
    code to be executed at least once
​}
while (condition);

How is JavaScript able to set up the condition before the function has declared fred to be a variable?

Other questions I've read pertain to var declarations inside the conditional.

5
  • There have to be dozens of dupetargets for this. Haven't found one yet. Commented Nov 3, 2017 at 12:25
  • Just put a console.log(fred) before prompt statement, you will notice that null is printed bazillion times before your prompt is even processed. That said fred is already hoisted since it has function scope. Commented Nov 3, 2017 at 12:29
  • @T.J. Crowder - I thought so, too. The 'Hoisted' JavaScript Variables question places the var declarations at the top - which makes sense except if you want the prompt to appear inside the loop. Commented Nov 3, 2017 at 12:32
  • @Sharpasamarble: Look again. The var x is in the middle of the function, and that (and hoisting) is central to the question (and the answers answer this question). Commented Nov 3, 2017 at 13:05
  • @T.J. Crowder - fair enough. 'Hoisted' JavaScript Variables provides a more complete answer as it demonstrates a function ignoring the variable x declared just above it, and hoisting its own. Commented Nov 4, 2017 at 15:05

3 Answers 3

5

From Mozilla documentation:

Variable declarations, wherever they occur, are processed before any code is executed.

and

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.

and a warning:

It's important to point out that the hoisting will affect the variable declaration, but not its value's initialization. The value will be indeed assigned when the assignment statement is reached:

Sign up to request clarification or add additional context in comments.

Comments

1

JavaScript is a bit odd when declaring variables with var; they are declared in the function scope, not the block scope, like other languages. If there is a var inside your function, anywhere, in the end the declaration is pulled up to the beginning (not the instantiation); so your code becomes:

function () {
    var fred;
    while(fred !== "stop"){
        fred = prompt("Should I stop or go?")
    };
}

When just declared, the variable exists and receives the value of undefined. If you want the more sensible block scoped variables, use let/const. Then you'd get the behavior you expected.

Comments

0

Before declaring fred, fred is undefined, so undefined !== "stop", which is true.

4 Comments

it isn't technically undeclared, javascript will 'hoist' var declarations into the top of the nearest function scope, so its declared, just not set to anything yet
@beeglebug: "undefined" != undeclared
It's hoisted, yes, but it's still undefined, so even if it never gets assigned a proper value, the condition would be true.
So why the downvote?!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.