you are viewing a single comment's thread
view the rest of the comments
[–] 13 points 1 year ago (12 children)

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.

  • source
  • parent
  • hideshow 12 child comments
  • [–] 13 points 1 year ago* (3 children)

    Explanation for nerdsThe 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.

  • source
  • parent
  • hideshow 3 child comments
  • [–] 5 points 1 year ago (1 child)
  • [–] 1 point 1 year ago

    Not wrong, but funnily enough, it's a linting rule win. I'd go nuts if I didn't have my type checks and my linters. My current L, though, is setting up the projects initially and dealing with the configuration files if I raw dog it, but that's a problem with ESLint configs and the ecosystem as a whole having to deal with those headaches. So in the end, the JS devs got clever and shifted the blame to the tooling. 😅

  • source
  • parent
  • [–] 4 points 1 year ago (4 children)

    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

  • source
  • parent
  • hideshow 4 child comments
  • [–] 2 points 1 year ago* (2 children)

    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.

  • source
  • parent
  • hideshow 2 child comments
  • [–] 1 point 1 year ago (1 child)

    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.

  • source
  • parent
  • hideshow 1 child comment
  • [–] 2 points 1 year ago (2 children)

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

  • source
  • parent
  • hideshow 2 child comments
  • [–] 1 point 1 year ago (1 child)

    Interesting, I'm not aware of any way they would affect compile errors. I'd be curious to know more.

  • source
  • parent
  • hideshow 1 child comment
  • [–] 2 points 1 year ago

    I don't have experience with how it affects JavaScript specifically, but independent of programming language, it usually removes the guesswork where the error might be.

    The thing is that compilers use fairly static rules for their grammar. So, even if you just typo a comma where there should've been a dot, then its grammar rules don't match anymore and it doesn't really know what to do with your code.
    To some degree, it's possible to say certain symbols just cannot appear in a specific place, but especially with a comma, it could be the start of the next element in a list, for example.

    Without semicolons, it's likely going to tell you that something's wrong between somewhere before your typo and the next closing brace (}). With semicolons, it can generally pinpoint the specific statement where the comma is wrong.
    This should also make analysis quicker while you're editing the code, as it only has to check one statement (or two, if you're inserting a new line and haven't typed the semicolon yet).

  • source
  • parent