JavaScript for…in vs for

前端 未结 22 1476
悲哀的现实
悲哀的现实 2020-11-22 07:15

Do you think there is a big difference in for...in and for loops? What kind of \"for\" do you prefer to use and why?

Let\'s say we have an array of associative array

22条回答
  •  无人共我
    2020-11-22 07:39

    I'd use the different methods based on how I wanted to reference the items.

    Use foreach if you just want the current item.

    Use for if you need an indexer to do relative comparisons. (I.e. how does this compare to the previous/next item?)

    I have never noticed a performance difference. I'd wait until having a performance issue before worrying about it.

提交回复
热议问题