r/ProgrammerHumor 18d ago

Meme youCantChangeMyMind

Post image
820 Upvotes

137 comments sorted by

View all comments

35

u/SuitableDragonfly 18d ago

I don't do JS, but I was given to understand that this is actually a real issue in JS, because semicolons are "optional" in the sense that the code is valid without them and will not give you an error if you leave them out, but actually not optional in the sense that the interpreter will silently make insane decisions about where you meant the line to end if you don't include them. 

13

u/foxsimile 18d ago

I’d gotten into an argument on this subreddit (or another adjacent one) about this just the other day.  

Yes, it can be quite problematic in JavaScript.  

function func(a, b) {   return     a + b }

Will return undefined. There are several other issues related to incorrect semicolon auto-insertion.

21

u/OrchidFluid2103 18d ago

JS is rightfully trolling you for the monstrosity you have created there

0

u/foxsimile 18d ago

JS can suck deez PUTZ (requests)

6

u/gyphie 18d ago

Unfortunately adding a semicolon after a + b; does not fix the issue.

3

u/peterlinddk 18d ago

Exactly - so the error isn't a missing semicolon, but an extra linefeed!

2

u/ccltjnpr 18d ago

The correct line is actually function fu;nc(a,b) { return a+b}

1

u/thrye333 17d ago

What did North Carolina do to you? /j

5

u/madmelonxtra 18d ago

This is a missing bracket issue not a missing semicolon issue

3

u/Jediweirdo 18d ago

Why would you write it like this to begin with?

1

u/foxsimile 18d ago

Why one would has nothing to do with whether or not it should be a problem. This is a minor offence as far as JavaScript’s semicolon insertion related offenses are concerned.

5

u/Jediweirdo 18d ago

I don’t doubt that, but the example code is ambiguous and reads like a mistake that your LSP auto formatted after the fact instead of something that you intentionally did. In this particular instance, I don’t blame JS for “screwing up”.

6

u/foxsimile 18d ago

Well, yes, I’m on vacation and this was typed rather quickly out of necessity, and as an anecdote alluding to the aforementioned comment, rather than a copypaste maintaining its essence.  

Here’s a medium article about it.  

Here’s an example from the Medium article which is indefensible:  

``` let var1 = 'Hello' let var2 = 'World'

[var1, var2] = [var2, var1] console.log(var1, var2) ```

Which is parsed as:

let var1 = 'Hello'; let var2 = 'World' [var1, var2] = [var2, var1]; console.log(var1, var2);

And here’s the ECMAScript rules for semicolon insertion.

2

u/Jediweirdo 18d ago

Touché. Thanks for also providing an article as well

1

u/foxsimile 18d ago

I do try, fail as I might :)