In line 10 to clearTimeout()
function there is passed timer
variable which is decleared outside moveBox
function and under it in the line 20. Why does the java script resolves timer
to a variable? Why it does see it?

- 17,363
- 67
- 204
- 344
-
1All that matters is that the variable is declared when the function actually runs. What's more, `var` declarations are always treated as if they appeared at the very start of a function. – Pointy Mar 26 '15 at 15:25
-
2When quoting code, quote it **as text**, not as a picture from your IDE. We can't copy pictures and add explanation to them (reasonably). – T.J. Crowder Mar 26 '15 at 15:27
-
It's known as variable "hoisting". – Andy Mar 26 '15 at 15:28
1 Answers
Variable declarations in JavaScript are processed before any step-by-step code in that scope, and functions declared inside other functions (and in the global scope) have access to variables declared in their containing scope.
So the variable exists because the declaration is processed beforehand, and the function has access to it because that's how scope works in JavaScript.
So what happens in your code happens in this order:
Variables
speed
,moveBox
, andtimer
are all declared and given the initial valueundefined
.Step by step code execution begins
speed
is assigned the value10
The function expression on the right of the
moveBox =
is evaluated and the resulting function reference is assigned tomoveBox
.The function expression being passed into
setInterval
is evaluated, and thensetInterval
is called with it and the value ofspeed
.The return value of
setInterval
is assigned totimer
(Some time later) The first call to
moveBox
occurs

- 1,031,962
- 187
- 1,923
- 1,875
-
-
@Cerbrus: There are two conflated things here (`var` hoisting and a question about scope), not just one, so unless there's another question asking *both* parts of that already (and there could well be)... And I'm rubbish at finding dupes. For instance, the dupe you chose doesn't address the question about hoisting at all, which was clearly a big part of the question. :-) – T.J. Crowder Mar 26 '15 at 15:30
-
1Hm, that's reasonable :-) Couldn't really find one that covers both subjects, either. – Cerbrus Mar 26 '15 at 15:31
-
@Cerbrus: I do need to get better at finding them, and we probably need better incentives for people to find them and better counter-incentives to prevent rep gain from answering blatant dupes. (The problem partially being, of course, how we define a blatant dupe...) :-) I sometimes answer something I'm sure is a dupe with a CW because I'm lazy. (And that's not right.) – T.J. Crowder Mar 26 '15 at 15:32
-
Something like _"If the question't closed as dupe within 15 minutes, votes are worth less rep"_? I'm not sure how you can reward dupe close votes without people going on a CV-rampage. – Cerbrus Mar 26 '15 at 15:34
-
1@Cerbrus: If the question is closed as a dupe within 15 minutes and stays closed, even zero rep on answers would suit me. Then the only incentive for answering if you're pretty sure something is a dupe is to tailor the answer to the OP (e.g., just being nice). Agreed on the CV thing, but finding and marking dupes is useful, we should find a way to encourage it. Perhaps if you mark it and four others do as well (none of this "one-vote-closes" stuff), and it stays closed as that dupe, rep gain there? – T.J. Crowder Mar 27 '15 at 07:23