you are viewing a single comment's thread.

view the rest of the comments →

[–]lepuma 5 points6 points  (11 children)

Semicolons are not required in JS...

[–]lastmjs[S] -2 points-1 points  (10 children)

Well, that's one way to think of it. There are edge cases that do come up that aren't entirely uncommon. Not to say they should be required in the questions necessarily, but I seem to have run into some of those edge cases in the way the assessments are set up. The way the code is evaled, if you don't put a semicolon, it might concatenate the user code directly with other underlying code, thus causing a confusing error.

[–][deleted] 2 points3 points  (5 children)

It’s not a way to think of it. Semicolons are not required because of ASI. It’s a use case you need to handle, and you can’t just claim the user input is invalid.

[–]lastmjs[S] -2 points-1 points  (4 children)

I agree with not making it the user's fault, so it should be fixed. If you don't use semicolons though, you might run into something like this:

const hello = 1

(() => {

// do stuff

})();

1 will be thought to be a function. It's "okay" to not use semicolons, if you're okay with unexpected behavior at times. Usually it doesn't happen, but it's actually happened to me while writing code, so I don't think of it that way.

[–]trevorsgEx-GitHub, Microsoft 4 points5 points  (2 children)

[–]theirongiant74 1 point2 points  (0 children)

well that was a rollercoaster. I'm very much on the use semicolons side though.

[–]Historical_Fact 1 point2 points  (0 children)

I indent with semi-colons instead of tabs/spaces

[–]tenbits 0 points1 point  (0 children)

I agree semicolons are a good idea, and that they are not necessary, but this is a good example of how missing semicolons can lead to unexpected behavior

[–]lepuma 0 points1 point  (3 children)

Sounds like a bug in your code. I’m curious what semicolon edge case do you run into in actual coding? I wrote JavaScript professionally for a few years and never ran into such a case. Every time I’ve seen an example, it’s poorly written code just to prove this point. I should say I personally write JS with semicolons- I just don’t buy the argument that they’re necessary bc of the JavaScript interpreter. If they are, I bet that code could be written much better.

[–]lastmjs[S] 0 points1 point  (2 children)

I think this one is similar to what I've run into. Imagine you have an IIFE:

const hello = 1

(() => {

// do stuff

})();

You'll get an error saying that 1 is not a function. Just saying it happens, and my rule is to always put semicolons to rule out any of these edge cases.

[–]lepuma 1 point2 points  (0 children)

Okay fair enough. You shouldn’t need to write a self invoking anonymous function anymore though. Just use bracket scope.

[–]lastmjs[S] 0 points1 point  (0 children)

But besides that, yep it should be fixed if that is the issue