Another JS Switch Trick

Поділитися
Вставка

КОМЕНТАРІ • 21

  • @FunctionGermany
    @FunctionGermany Рік тому +26

    breaks principle of least surprise, therefore i would not recommend this.

    • @apidas
      @apidas Рік тому +4

      ah you must be fun to work with 😂

    • @gordonfreimann
      @gordonfreimann Рік тому

      have you forgotten to take your meds again?

    • @FunctionGermany
      @FunctionGermany Рік тому +21

      @@apidas when production is down and i need to fix a bug fast i don't want to run into "fun" code.

    • @TylerJBrown192
      @TylerJBrown192 Рік тому +4

      1000% agree. While this method of utilizing a switch _could_ result in less overall LOC due to not needing curly braces for an if statement declaration, it is inherently less extensible as sequential if/else statements.
      This is something I'd look at in a codebase, and after 10 seconds go "huh okay, cool I guess", and move on with my day very slightly annoyed that I had to spend those 10 seconds processing the deviation in standards haha

    • @apidas
      @apidas Рік тому

      @@FunctionGermanyyeah, keep your for loop and require statements

  • @BoonBoonitube
    @BoonBoonitube Рік тому +3

    C# has much more flexible switch statements, since I regularly use both C# and JS, this trick goes a ways into making JS switches as flexible as in C#

  • @offroaders123
    @offroaders123 Рік тому +3

    Discovered this recently too, it works really well for some instances! I haven't had much experience in Rust, but I heard the match statement kind of works like this. It would be great to have it in JS too!

  • @dinckelman
    @dinckelman Рік тому

    I had no idea you can do that!

  • @real_ouss
    @real_ouss Рік тому +7

    Just use an if statement dude 😂

    • @dellavita3463
      @dellavita3463 11 місяців тому +1

      😂 that’s what I thought

    • @les2997
      @les2997 9 місяців тому

      It might be a little neater than if then else, but not by much

  • @Shizzymynizzy
    @Shizzymynizzy Рік тому

    Very cool

  • @Tarabass
    @Tarabass 8 місяців тому

    Smelly code

  • @idontwhy3132
    @idontwhy3132 11 місяців тому

    neat

  • @ChaimMosheStern
    @ChaimMosheStern 9 місяців тому

    Or you can just do if else

  • @lucas.codes.tutorials
    @lucas.codes.tutorials 11 місяців тому

    This is not as performative as an if-else chain because every time it is executed, the interpreter will execute the pointless comparison "true==true", so besides the comparisons executed on each case, an extra unnecessary comparison will be executed