zjmiller
zjmiller

Reputation: 2787

Why don't switch statements work with arrays?

The following alerts nothing. I assume everyone can see what I'm trying to accomplish. Does anyone know what's going wrong?

var myarray = ['foo', 'bar'];

switch (myarray) {

    case ['foo', 'bar']:
        alert('foobar');
        break;

    case ['foo', 'foo']:
        alert('foofoo');
        break;
}

Upvotes: 0

Views: 80

Answers (1)

James Montagne
James Montagne

Reputation: 78690

Because ['foo', 'bar'] does not equal another instance of ['foo', 'bar']. They are two distinct objects which happen to contain the same information.

Upvotes: 8

Related Questions