はい、違いがあります。変数が宣言されていない場合は
1-
:
var foo; // hoist foo (declare it at top)
for (foo = something; …; …) // but doesn't assign the value at top, it will assign it where it was before the hoisting
statement;
ではなく、に相当:
var foo = something; // wrong assumption: it should not move the assignemet to top too, it should move just the declaration
for (; …; …)
statement;
証明
for (var foo = something; …; …)
statement;
は同等ですエラーがスローされます。
2-
変数に値が代入されることはありません場合は、その値がundefined
です:
var foo;
console.log(foo);
3-
割り当てトップに宣言(ホイスト)を移動ではなく:
var foo; // declared first so it doesn't throw an error in the next line
console.log(foo); // undefined so the assignment is still after this line (still at the same place before hoisting)
var foo = "Hello, world!"; // assignment here to justify the logged undefined value
'foo':
それと等価ですどちらの場合でもまだ関数スコープですが、それは正しい。 – Tomalak