for … in loop with string array outputs indices

前端 未结 4 479
孤城傲影
孤城傲影 2020-12-03 11:16

When I write some javascript such as this:

var words = [\'word1\', \'word2\', \'word3\']

for (word in words) {
    console.log(word)
}

The

4条回答
  •  庸人自扰
    2020-12-03 11:44

    For ... in is intended for objects -- see this question. Apparently it can (and is) used for arrays, but this has a few risks you may not want.

提交回复
热议问题