Why does typeof null work incorrectly in switch statements?

It is well known that

typeof null

returns an "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 doesn't the "object" returned by typeof null cause the first branch of the case statement to execute?

+3
source share
1 answer

You are missing breakfor the first case - so it falls into the register defaultand returns 3.

switch(typeof null){
    case "object": 
        1; 
        break;
    default: 
        3;
}
+9
source

Source: https://habr.com/ru/post/1748091/


All Articles