Nesting Switch Statements

I want to nest switch statement but i get errors, can someone explain please!

      const var1 = "hello1";
      const var2 = "hello2";
      switch(var1) {
        case(var2)
        : {
          switch(var2) {
            case("hello")
            : console.log("hello");
          }
        }
        default
        : console.log("default")
      }

the error is something like this: Type annotations can only be used in TypeScript files.

Your formatting might be confusing the linter?

      const var1 = "hello1";
      const var2 = "hello2";
      switch(var1) {
        case(var2): {
          switch(var2) {
            case("hello"): console.log("hello");
          }
        }
        default: console.log("default")
      }

Nested switches are probably a bad idea in 99.999% of cases…

That said, this is what your code currently says

      const var1 = "hello1";
      const var2 = "hello2";
      if (var1 === var2) {
          if (var2 === "hello") {
              console.log("hello");
          }
      } else {
          console.log("default")
      }

nope, still the error pops up :weary:

woops, no ()s in cases

1 Like

:man_facepalming:
:man_facepalming:

I do stuff like that all the time because I switch between languages or forget the syntax for a specific language feature.

Thanks mate for the solution, so appreciated!
Good Luck!

1 Like