Last active
July 18, 2019 10:50
-
-
Save chuck0523/fb80faf77842a427e02692eef81b7658 to your computer and use it in GitHub Desktop.
for文とforEachにおける配列イテレーション差異
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 要素数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