Thursday, February 02, 2012

IE8 Gotcha With Function Arguments

Got screwed over by this problem today. Good ol’ IE!

You can’t enumerate the arguments array using a for..in loop in IE8 and below – the loop will not execute.

function testArguments() {
    for (var index in arguments) {
        var argument = arguments[index];
        // this loop is not executed in IE8 and below
    }
}

Use a standard for loop and sanity shall be restored!

function testArguments() {
    for (var index = 0; index < arguments.length; index++) {
        var argument = arguments[index];
        // success!
    }
}