2

I have following code snippets.

var caseObj = function () {

}

switch (typeof caseObj) {

    case "function":
        console.log("it is function");

    case "object":

        console.log("It is object now");
}

Its output is

it is function.
It is object now.

But typeof caseObj gives output function but it still evalutes case "object" case also.

How it is possible? Am I doing wrong anythig?

EDIT :

typeof caseObj is giving function,So it executing that case but it also executing object case.Why this strange behavior?

Tushar
  • 85,780
  • 21
  • 159
  • 179
RIYAJ KHAN
  • 15,032
  • 5
  • 31
  • 53

3 Answers3

6

The problem is not with the typeof, but you've missed the break statement in the case. That'll make the case like function OR object and execute the block of both the cases.

You missed the break; statement for the cases. This is the reason, of falling out in the next case.

The break statement terminates the current loop, switch, or label statement and transfers program control to the statement following the terminated statement.

var caseObj = function() {

}

switch (typeof caseObj) {

  case "function":
    document.write("it is function");
    break;

  case "object":

    document.write("It is object now");
    break;
}

From the comments in the answer:

But without break it also fall-down if there is not matching case and exit from switch.But it executing case "object": statment as well.Why?

From MDN

If a match is found, the program executes the associated statements. If multiple cases match the provided value, the first case that matches is selected, even if the cases are not equal to each other.

The optional break statement associated with each case label ensures that the program breaks out of switch once the matched statement is executed and continues execution at the statement following switch. If break is omitted, the program continues execution at the next statement in the switch statement.

Community
  • 1
  • 1
Tushar
  • 85,780
  • 21
  • 159
  • 179
  • Why there is need of break? typeof caseObj is "function" and not "object".Why it is evaluating last case also? – RIYAJ KHAN Sep 03 '15 at 07:13
  • @RIYAJKHAN `break` allows to get control out of the `switch` – Tushar Sep 03 '15 at 07:14
  • without break it will keep evaluating the condition following the perticular case.. – Amit Sharma Sep 03 '15 at 07:14
  • But without break it also fall-down if there is not matching case and exit from switch.But it executing case "object": statment as well.Why? – RIYAJ KHAN Sep 03 '15 at 07:16
  • @RIYAJKHAN Because, parser thinks that the two cases are combined, since there is no `break` in between them – Tushar Sep 03 '15 at 07:19
  • 1
    I ran into this issue with `switch` and found that if break is missing the other cases are executed even if they do not match. Seems completely absurd. Is there a logic behind this or its just a bug in the language ?? – Saurabh Tiwari May 17 '17 at 06:52
2

The switch execution begins at the first case that matches the requested value, beginning from the top.

Then, unless you use a break somewhere, it will continue to execute all cases from the first that matches to the bottom.

As an example, if you invert your 2 cases in your example, it will only output "it is function".

tomaoq
  • 3,028
  • 2
  • 18
  • 25
0

You didn't have a break statement ending each case clause. When you do this, the control flow passes to the statements of the next case clause, not matter the case condition is not satisfied. For instance if you have:

var x = 1;
switch(x) {
   case 0: console.log(0);
   case 1: console.log(1);
   case 2: console.log(2);
   case 3: console.log(2);
}

The output would be:

1
2
3

but if you put the break in each case

var x = 1;
switch(x) {
   case 0: console.log(0);break;
   case 1: console.log(1);break;
   case 2: console.log(2);break;
   case 3: console.log(2);break;
}

the output would be just 1

NValchev
  • 2,855
  • 2
  • 15
  • 17