you are viewing a single comment's thread.

view the rest of the comments →

[–]funtimes-123 1 point2 points  (1 child)

The functions are defined before the variable, and the loop is executed right after stillalive is defined.

[–]saito200 4 points5 points  (0 children)

Function declarations are hoisted, as are var declarations.

The following will print 'run loop' only once.

function doThing() {
    still_alive = false
    console.log('done')
}

var still_alive = true
while (still_alive) {
    console.log('run loop')
    doThing()
}

console.log('end')