• ScintillatingStruthio@programming.dev
    link
    fedilink
    English
    arrow-up
    1
    ·
    3 months ago

    Neither Javascript nor Typescript require semicolon, it is entirely a stylistic choice except in very rare circumstances that do not come up in normal code.

    • Lemminary@lemmy.world
      link
      fedilink
      arrow-up
      3
      ·
      edit-2
      3 months ago
      Explanation for nerds

      The reason is the JS compiler removes whitespace and introduces semicolons only “where necessary”.

      So writing

      function myFn() {
        return true;
      }
      

      Is not the same as

      function myFn() {
        return 
          true;
      }
      

      Because the compiler will see that and make it:

      function myFn() { return; true; }
      

      You big ol’ nerd. Tee-hee.

      • Ephera@lemmy.ml
        link
        fedilink
        English
        arrow-up
        1
        ·
        3 months ago

        That’s terrifying, especially in JS where no type system will fuck you up for returning nothing when you should’ve returned a boolean.

    • Maiq@lemy.lol
      link
      fedilink
      arrow-up
      1
      ·
      3 months ago

      That’s good to know. Don’t know how I didn’t know this. Been writing JS since 2000. Always just used them I guess. Ecmascripts look funny to me without them

      • ScintillatingStruthio@programming.dev
        link
        fedilink
        English
        arrow-up
        1
        ·
        edit-2
        2 months ago

        Fair enough, I like it better without but I don’t have a strong preference and have no issue adapting to whatever the style of the repo is.

        I learned about it researching tools to automatically enforce formatting style and came across StandardJS, which eliminates them by default.

        • Maiq@lemy.lol
          link
          fedilink
          arrow-up
          1
          ·
          2 months ago

          I can see the benefit of matching style when working with others. I only code for myself and never had to worry about conformity for project consistency.

          It is good to learn new things.

          I’m sure I have some coding habitats that would annoy others.

          • ScintillatingStruthio@programming.dev
            link
            fedilink
            English
            arrow-up
            1
            ·
            2 months ago

            Consistent styling helps make the actual meaningful changes easier to spot. Probably also useful for your own commit history when working solo in a repo, but most useful in a team, yeah!

    • Ephera@lemmy.ml
      link
      fedilink
      English
      arrow-up
      0
      ·
      3 months ago

      Hmm, a webdev colleague said he’d normally prefer without semicolons, but used them anyways for better compile errors.