JavaScript JavaScript while loop accepts condition before var declaration [duplicate] javascript while loop break,javascript while loop example,javascript while

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.

Answer:1

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:

Answer:2

I have a simple requirement: Based on the response of a user on a particular yes no question, show him another question. The issue is I am using Bootstrap forms so all my div classes are named form-...

I have a simple requirement: Based on the response of a user on a particular yes no question, show him another question. The issue is I am using Bootstrap forms so all my div classes are named form-...

I'm having trouble with IntelliJ Idea's code completion and syntax checking and JavaScript. I have the following (simplified) code for a Singleton object: var MySingleton = new function() { var ...

I'm having trouble with IntelliJ Idea's code completion and syntax checking and JavaScript. I have the following (simplified) code for a Singleton object: var MySingleton = new function() { var ...

I have a nested array. It is about user information. I need to take the age from the user and add it to my array. I have tried some code approch. Did not work. Here is the Array var userData = [ { ...

I have a nested array. It is about user information. I need to take the age from the user and add it to my array. I have tried some code approch. Did not work. Here is the Array var userData = [ { ...

  1. add element to nested list python
  2. add element to nested dictionary python
  3. add element to nested array mongodb
  4. add element to nested table oracle
  5. add element in nested list
  6. add element to nested dictionary
  7. add element to nested table
  8. add element to nested array

I try to update the HandsonTable settings on the fly. The settings seem to update but the rendered table still uses the previous settings. Example: http://jsfiddle.net/kAFWA/30/ updateSettings ...

I try to update the HandsonTable settings on the fly. The settings seem to update but the rendered table still uses the previous settings. Example: http://jsfiddle.net/kAFWA/30/ updateSettings ...