JavaScript Switch Expressions???

Поделиться
HTML-код
  • Опубликовано: 28 июн 2023
  • Check out my videos for more JS and TS content!
    My Links
    shaky.sh
    shaky.sh/tools
    / andrew8088
    mastodon.social/@andrew8088
  • НаукаНаука

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

  • @tomshieff
    @tomshieff Год назад +23

    I really love IIFE's, but imagine a new intern joins your team and the code is full of these. Pretty sure they would die 😆

    • @69k_gold
      @69k_gold Год назад

      Switch is a pretty basic functionality

  • @EpKjelltzer
    @EpKjelltzer Год назад +18

    As much as I wish this was the answer to JS's missing pattern matching, I think this is too clever. The syntax is difficult to understand, especially for beginners, and it still has all the fragility of a traditional switch statement, including fall through if you forget a return and no exhaustiveness guarantee.

  • @thomasogara
    @thomasogara Год назад +7

    I wish javascript had everything-expressions (switch, if, just everything)

  • @gerardmarquinarubio9492
    @gerardmarquinarubio9492 11 месяцев назад +4

    Me: Can we have pattern matching?
    Mom: We have pattern matching at home.
    Pattern matching at home:

  • @po9968
    @po9968 Год назад +2

    Pretty cool! It makes switch a functionnal expression as a ternary operator. And the syntax is pretty crystal clear

  • @swapnilbhattacharjee183
    @swapnilbhattacharjee183 Год назад +3

    Even though I don't write vanilla JS on any projects now, I do this all the time in TS.
    I find this behaviour particularly useful in cases where I have to do a conditional assignment or initialisation. The benefit here is that the LSP will infer the variable as having a union type of all the return values rather than having a very broad type like number or string and that gives you a lot more of that sweet, sweet autocomplete.
    I also use IIFE initialisation for throwaway functions that I don't have any reuse scope for.
    One more advantage of this pattern is the need to declare fewer scoped variables whenever you need to transform the data from an external source: api, dom, fs, db, etc. into a more palatable form that the application can consume.

  • @Matt23488
    @Matt23488 Год назад +3

    I think it depends. Declarative code is great and JS/TS needs this as a feature, but the extra syntax required for this trick may make the code harder to read, depending on the surrounding code. I'd say use it, but sparingly.

  • @noriller
    @noriller Год назад +2

    for something really small, yes (i use it often). but for other stuff, better to just make a function and call it

    • @thi-m10
      @thi-m10 Год назад +1

      I agree. The function's name by it self would improve the readabiity and it could be reused

  • @GiveMeSomeMeshuggah
    @GiveMeSomeMeshuggah Год назад +1

    Since it’s a closure, I believe that function would be allocated on the heap every single time it runs. Gotta love javascript’s memory model.

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

    Dart now has a switch expression with modern pattern matching. I really like it.

  • @theklr
    @theklr Год назад +2

    I think there's a mix. I see why people say it's clever and don't want to use it, but I also can see how this can kill nested if else issues. If you can make a case for it on the team, everyone can understand it, and it's testable? Don't see the issue.

  • @odilf_
    @odilf_ Год назад +2

    actually looks cool

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

    I think wrapping with a function and giving a coherent name, then invoking the function separately is the way I would go instead of the IIFE. If the code is in a hot path too, not having the overhead of creating a function every time could actually be significant

  • @jon1867
    @jon1867 11 месяцев назад

    ts-pattern is nice too! But I used IIF's like this all the time too

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

    ts-pattern is the way

  • @chookingvid
    @chookingvid Месяц назад

    I prefer nested ternaries. They are not hard to read if formatted properly with line breaks and indents.

  • @paintingtheweb
    @paintingtheweb Год назад +2

    I prefer to just use an object and return a dynamic property.
    ...
    const priorities = {...}
    const p = priorities[job.priority] ?? defaultPriority
    ...

    • @thi-m10
      @thi-m10 Год назад +2

      The range situation it'd not fit. But I also prefer to use an object 'cause it's hashed in JS;

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

    How about just making if statement an expression like some other programming languages did years ago? 😂

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

    BTW, Java 17 has switch expressions

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

      Can Java 17 run in the browser?

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

      @@invinciblemode wasm.. duh!!

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

      @@deadlyecho for making UI? No thanks lmao

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

      @@invinciblemode well you can still write your lovely UI in HTML and Tailwind and handle events in Java or even Rust... 😉

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

      @@deadlyecho disgusting 🤮