Spot the bug - for/in and JavaScript


Written on Monday, September 20, 2010 by
I'm poking around in the JSOQ source this week and came across this gem.

var array = [
    { id: 1, num: 2 },
    { id: 2, num: 3}
];

// find the item with num == 2
for(var item in array) {
    if( item.num == 2 ) {
        alert(item.id);
    }
}
The code above isn't calling alert(item.id). Why is this?
Back

Let's talk about this

 


The opinions expressed herein are my own and do not represent my employer's view in any way.


Creative Commons License