Originally Posted by Old Pedant
Yes, I absolutely agree that your example is the pathological one.
It does illustrate that it doesn't matter where in the function you declare the variable because the variable exists from the start of the function.
Declaring where first used still conceals the fact that the variable still exists from the start of the function and isn't created at the point where you declare it. This is different from the way some other languages work where a variable is created when it is first used. I can't think of an example of where this makes a difference but it obviously does in some situations or the syntax checker at jslint.com wouldn't give a warning if you use multiple var statements in the one function.
Consider the following:
var myArray = [[0, 1, 2, 3, 4],[5, 6, 7, 8, 9],[10, 11, 12, 13, 14],[15, 16, 17, 18, 19]];
for (var i = 0; i < myArray.length; ++i)
var subArray = myArray[i];
for (var j = 0; j < subArray.length; ++j)
str = myArray[i][j];
That code will refuse to run in modern browsers because one of the variables is not declared but you don't have easy access to a list of which are declared. The error console will tell you the first variable that it found that you forgot to declare but will still not assist much in finding the first place it is used.
Declaring on first use can also lead to confusing situations such as:
if (somecondition) var mynewvar = 'true condition text';
else mynewvar = 'false condition text';
A newbie would probably place a completely unnecessary var in front of the variable name on the second line not realising that the first line declares it even if that statement never runs.