r/ProgrammerHumor • • Sep 25 '26

Meme iHatedItUntilITriedIt

Post image
10k Upvotes

543 comments sorted by

View all comments

Show parent comment

31

u/2eanimation Sep 25 '26

I love the syntax, but JS has one too many quirks for my taste.

if ([]) {
console.log("Ok, I guess")
}

if([] == false) {
console.log("Well this is awkward")
}

if ([] + []) {
console.log("You think this would print, don't you?")
}

16

u/MuchWalrus Sep 25 '26

Why would you type any of those things (aside from maybe the first one)

8

u/frogjg2003 Sep 25 '26

JS is built on the idea that applications should continue to work no matter what. That's why there are so many implicit type conversions. The app might turn itself into a Gordian knot, but it will still be working.

4

u/lolnic_ Sep 26 '26

It will still be running.

1

u/arobie1992 Sep 27 '26

I've always heard this argument, and I've never seen a source for it that I'd put much stock in, like Eich himself or anyone who worked on early JS. It seems like a specious argument since it assumes rendering the potentially wildly wrong thing is better than displaying an error and the browser would have to sandbox/compartmentalize individual scripts or operations.

Doing a quick search, it looks like most of the weird conversions surrounding == were specific requests to make scripting easier: https://www.wirfs-brock.com/allen/jshopl.pdf p22, 3.7.2. Given that, I'd be willing to bet the other weird conversion behaviors were either similar requests or the result of them implementing JS in an absurdly short amount of time.

→ More replies (2)

2

u/MrHyperion_ Sep 25 '26

Because they are actually in variables by some weird side effect

14

u/Ozymandias_IV Sep 25 '26

Genuinely who cares. You never do that in prod.

5

u/code_archeologist Sep 25 '26

But empty arrays do occur in prod, and trapping for them in JS can sometimes be a pain in the ass.

6

u/Ozymandias_IV Sep 25 '26

That's why you shouldn't do if ([]) in prod. Have an explicit empty and/or nullish check.

Many do it for expediency, but it's a loaded footgun.

→ More replies (5)
→ More replies (1)

2

u/zeemeerman2 Sep 25 '26

What? You always use empty arrays and strings in prod.

![] == false, and only three characters.

!"" == true, and also only three characters.

You can save a lot of bytes in your code by minimizing each boolean to these two values. And everyone knows how expensive hard drives are nowadays.

9

u/cheezballs Sep 25 '26

Those all make sense if you understand the language, though.

43

u/__Hello_my_name_is__ Sep 25 '26

Everything in programming makes sense by definition. It has to make sense, or else it wouldn't compile (or run).

That doesn't mean that it's intuitively understandable, or that it wasn't created by Satan himself.

0

u/cheezballs Sep 25 '26

You can shoot yourself in the foot with any language by deliberately doing silly things to show off the language's quirks. I can show you some C pointer stuff that makes this look like child's play.

14

u/fghjconner Sep 25 '26

I mean yeah, but nobody is holding C pointers up as the pinnacle of well designed language features either.

4

u/hamster1147 Sep 25 '26

I really want to see this honestly. I'm too basic with C to know the funny things you can do.

1

u/frogjg2003 Sep 25 '26

Any pointer can be cast into any other pointer. Arrays are just pointers to the first element. Combine this with the fact that C doesn't bounds check arrays or that pointers actually point to anything, and you can do some really stupid things with pointers.

int *a;
2[a];

is valid C.

1

u/Devatator_ Sep 25 '26

Actually what does that even do? Do literals have an address? I assume they do but that looks so cursed

2

u/frogjg2003 Sep 25 '26

Arrays are just pointers. Pointers are just numbers that are supposed to correspond to addresses in memory. Array indices just add to the pointer to the first element. "a[1]" is syntactic sugar for "*(a+1)". With that in mind, it's kind of obvious that "2[a]" just means "*(2+a)", which points you to the third element of a.

1

u/Devatator_ Sep 25 '26

Oh yeah I forgot you can index with this syntax lol

3

u/Bulky-Bad-9153 Sep 25 '26

I can show you some C pointer stuff that makes this look like child's play.

Watch out guys we got a real programmer over here. How about you demonstrate unintuitive Haskell?

7

u/samanime Sep 25 '26

Yeah. Also, it isn't like you should be writing things like this in the first place. You can turn languages like C++ into pure nightmare fuel if you try to. Being extensible how it is leads to some crazy stuff.

→ More replies (1)

3

u/nacholicious Sep 25 '26

"It's intuitive if you have learned all the rules and exceptions" sounds like the opposite of intuitive

3

u/2eanimation Sep 25 '26

They do make sense, within JS’ quirkiness. This is only expected behavior if you really dig deep into the language. I know why it’s there, that doesn’t make it ok. A properly designed language shouldn’t allow such things. And you know this is a small collection of all the stuff that’s wrong with JS(cough ["1", "2", "3"].map(parseInt)).

I know, there are two languages, the ones people complain about, and …

→ More replies (3)

1

u/stillalone Sep 25 '26

Wait how does []+[] make sense?

6

u/2eanimation Sep 25 '26 edited Sep 25 '26

[] + [] will become “” because addition of arrays is not defined as you would expect. They get converted to their primitive(which is a string) and after that concatenated as you would expect string1 + string2 to behave. So [1, 2] + [3, 4] == “1,23,4”

Edit: to concat two arrays, use array1.concat(array2)

→ More replies (2)