Another JS Switch Trick

Поделиться
HTML-код
  • Опубликовано: 13 июл 2023
  • #javascript #coding #programming
  • НаукаНаука

Комментарии • 21

  • @FunctionGermany
    @FunctionGermany Год назад +26

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

    • @apidas
      @apidas Год назад +5

      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 Год назад +2

      😂 that’s what I thought

    • @les2997
      @les2997 10 месяцев назад

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

  • @Shizzymynizzy
    @Shizzymynizzy Год назад

    Very cool

  • @idontwhy3132
    @idontwhy3132 Год назад

    neat

  • @Tarabass
    @Tarabass 8 месяцев назад

    Smelly code

  • @ChaimMosheStern
    @ChaimMosheStern 10 месяцев назад

    Or you can just do if else

  • @lucas.codes.tutorials
    @lucas.codes.tutorials Год назад

    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