Skip to content

Instantly share code, notes, and snippets.

@chuck0523
Last active July 18, 2019 10:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chuck0523/fb80faf77842a427e02692eef81b7658 to your computer and use it in GitHub Desktop.
Save chuck0523/fb80faf77842a427e02692eef81b7658 to your computer and use it in GitHub Desktop.
for文とforEachにおける配列イテレーション差異
// 要素数10の配列を作成
const ary = new Array(10)
// 0番目と3番目の要素に文字列を設定
ary[0] = "foo"
ary[3] = "bar"
// 10回標準出力される
for (let i = 0; i < ary.length; i++) {
console.log(ary[i])
}
// foo
// undefined
// undefined
// bar
// undefined
// undefined
// undefined
// undefined
// undefined
// undefined
// undefined
// 2回照準出力される
ary.forEach((item) => {
console.log(item)
})
// foo
// bar
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment