views:

36

answers:

1

It is commonly known that

typeof null

returns "object".

However, I have a piece of code that looks like this:

switch(typeof null){
    case "object": 
        1; 
    default: 
        3;
}

This code returns 3.

Why does "object" as returned by typeof null not cause the first branch of the case statement to be executed?

+4  A: 

You're missing break for the first case - so it falls through to the default case and returns 3.

switch(typeof null){
    case "object": 
        1; 
        break;
    default: 
        3;
}
Amarghosh
A classic blunder!Thank you
a_david