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.

  • There have to be dozens of dupetargets for this. Haven't found one yet. – T.J. Crowder Nov 03 '17 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. – gurvinder372 Nov 03 '17 at 12:29
  • @T.J. Crowder - I thought so, too. The ['Hoisted' JavaScript Variables](https://stackoverflow.com/questions/29667199/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. – Sharpasamarble Nov 03 '17 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). – T.J. Crowder Nov 03 '17 at 13:05
  • @T.J. Crowder - fair enough. ['Hoisted' JavaScript Variables](https://stackoverflow.com/questions/29667199/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. – Sharpasamarble Nov 04 '17 at 15:05

3 Answers3

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:

gpeche
  • 21,974
  • 5
  • 38
  • 51
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.

Luan Nico
  • 5,376
  • 2
  • 30
  • 60
0

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

Constantin Groß
  • 10,719
  • 4
  • 24
  • 50