Skip to main content
Stack Overflow
  1. About
  2. For Teams

Return to Answer

Post Timeline

added 204 characters in body
Source Link
Joe
  • 48k
  • 37
  • 166
  • 262

Because an array can have holes, and therefore you can iterate over the length and not all values will exist.

x = new Array()
[]
x[0] = "zero"
"zero"
x[5] = "five"
"five"
x
["zero", undefined ×ばつ 4, "five"]
3 in x
false
x.length
6
for (var i = 0; i < x.length; i++) { console.log(i, i in x, x[i])}
0 true "zero"
1 false undefined
2 false undefined
3 false undefined
4 false undefined
5 true "five"

Because an array can have holes, and therefore you can iterate over the length and not all values will exist.

x = new Array()
[]
x[0] = "zero"
"zero"
x[5] = "five"
"five"
x
["zero", undefined ×ばつ 4, "five"]
3 in x
false
x.length
6

Because an array can have holes, and therefore you can iterate over the length and not all values will exist.

x = new Array()
[]
x[0] = "zero"
"zero"
x[5] = "five"
"five"
x
["zero", undefined ×ばつ 4, "five"]
3 in x
false
x.length
6
for (var i = 0; i < x.length; i++) { console.log(i, i in x, x[i])}
0 true "zero"
1 false undefined
2 false undefined
3 false undefined
4 false undefined
5 true "five"
Source Link
Joe
  • 48k
  • 37
  • 166
  • 262

Because an array can have holes, and therefore you can iterate over the length and not all values will exist.

x = new Array()
[]
x[0] = "zero"
"zero"
x[5] = "five"
"five"
x
["zero", undefined ×ばつ 4, "five"]
3 in x
false
x.length
6
lang-js

AltStyle によって変換されたページ (->オリジナル) /