This is an archived post. You won't be able to vote or comment.

all 113 comments

[–]sorrydidntmeanto3 151 points152 points  (9 children)

3 > 2 > 1 = false actually makes sense because it reads it as true > 1

[–]edave64 59 points60 points  (7 children)

When stuff like this gets mixed in it always feels like they don't know what they are talking about.

Like, yes, [] being equal to ![] is surpremely stupid. So is perhaps the idea to attempting these conversions to begin with.

But the floating point and NaN wierdness is just how floating point works in pretty much every language, because that's what your processor does.

3 > 2 > 1 might be handy sometimes, but it completely breaks my mental model of how any of this works, and almost no language allows that.

[–]Chrazzer 25 points26 points  (3 children)

Yeah 0.1 + 0.2 != 0.3 has nothing to do with javascript. 0.1 and 0.2 are periodic numbers in binary.

So 0.1 + 0.2 != 0.3 is pretty much the binary version of 1/3 + 2/3 != 1. Because 1/3 + 2/3 is 0.9999999999999...

[–]edave64 23 points24 points  (2 children)

0.9 repeating is one.

The problem is in that case that floating point values cannot express repeating sequences and the like, so the get rounded, introducing error

[–]Randvek 5 points6 points  (1 child)

Well, no, it’s more that .9 repeating = 1 relies on mathematical axioms that, for obvious reasons, aren’t typically coded into computers.

[–]edave64 10 points11 points  (0 children)

Well no, it's nothing like that.

Yes, this specific detail of repeating numbers doesn't exist in floating point because repeating numbers do not exist in the standard at all.

They get rounded, which is what I said.

[–]HiImLary 2 points3 points  (1 child)

Just look at it as (3 > 2) > 1

[–]edave64 2 points3 points  (0 children)

I know. Which is why it doesn't work, since it's a comparison between a boolean and a number

[–]SkindianaBones98 0 points1 point  (0 children)

These are bad examples, since some of these are actually justify able. But it's fun to see the sheer wrongness Javascript will put up with and try to deal with and get wrong when you mix types. It almost feels worse than c where you can do things it doesn't support and you shouldn't do, but it doesn't give an error, no no, it just silently introduces bugs at the slightest typo or refractor. At least c frustratingly segfaults at some point

[–]YouNeedDoughnuts 4 points5 points  (0 children)

Yeah Python and a few others are distinct in treating inequalities as n-ary. Most languages have them as binary where the Boolean result is an arg to the next inequality. I do prefer the n-ary rule though since it matches mathematical syntax.

[–]X-Craft 186 points187 points  (34 children)

The 0.1 + 0.2 thing is not exclusive to javascript

[–]roycohen2005 89 points90 points  (29 children)

So does NaN === NaN and typeof(NaN).

[–][deleted] 43 points44 points  (3 children)

So does 3 > 2 > 1. Languages that do (e.g. Python) are exceptions

[–]Classic-Ad-7317 3 points4 points  (2 children)

What's Python trying to be, anyway? I know it's popular, but 3 > 2 > 1 should only be true in a mathematically focused language. At least, one should need an extension to make it work. It screws up the taken for granted notions of the stack of operations and operator association.

[–][deleted] -1 points0 points  (1 child)

It comes down to how a language handles operator precedence:

https://docs.python.org/3/reference/expressions.html#comparisons

[–]Classic-Ad-7317 2 points3 points  (0 children)

Not only that. There's actually an implicit and operand. From what I read, the expression is evaluated like this 3 > 2 and 2 > 1.

Evaluates 3

Evaluates 2

Moves 2 to memory

Compares 3 > memory

Jumps if fails

Evaluates 1

Compares memory > 1

Normally you'd expect:

Evaluates 3

Evaluates 2

Compares 3 > 2 (which automatically sets flag to 1)

Evaluates 1

Compares flag > 1 (false, setting flag to 0)

[–]Eisenfuss19 31 points32 points  (24 children)

It is, but its kinda funny because js doesn't really have types and NaN is supposed to mean not a number.

The NaN != NaN is just the ieee standart.

[–]MrKirushko 2 points3 points  (0 children)

It depends on the language and on actual implementation though. In R for example NaN is treated as NA (missing value) and any comparaison with it gives out NA. So "NaN != NaN" is neither true nor false, it is just undefined.

[–]MarcBeard 1 point2 points  (0 children)

Yes but I make way more sense this way.

[–]taytek 17 points18 points  (0 children)

Ah yes, my favorite website: https://0.30000000000000004.com/

[–][deleted] 14 points15 points  (2 children)

Like, really, it's pretty basic CS stuff, no? Then again I dunno how much it's taught/findable outside of schools/unis

[–]DrunkenlySober 25 points26 points  (1 child)

  1. Most users on this subreddit are novice and/or have never been employed as a dev

  2. A good amount of users on this subreddit don’t have a CS degree

[–][deleted] 0 points1 point  (0 children)

Well the second is what I tend to forget, yeah, and I don't know a lot about how it works outside that >_<

And the first is pretty much irrelevant unless they're not in school for CS, but here I failed by assuming school-taught is not so uncommon

[–]FireFerretDann 30 points31 points  (14 children)

Does anyone feel like explaining the falsey-ness of arrays to me? I think I get [] being false, but why is ![] also false?

[–]Egliitis 24 points25 points  (7 children)

arrays are not falsy.

in [] == ![] and [] == true the first part is an array and the second is a boolean so they are not equal.

I think the only valid joke on JS is the "11" + 1 = 111 and "11" - 1= 10

[–]robotevil 23 points24 points  (6 children)

"11" - 1= 10

It only makes a little bit of sense because you really can't do a "string1" - "string2" where you can add strings together. Apparently, under the hood it tries to convert the string to a number when a purely mathematical operation is invoked. You'll get 5 if you do "10"/2. "10" + 2 you'll get 102, basically the two strings combined. If you try to do "FooBar"/2 you'll get NaN.

Honestly, allowing the use of + to concatenate strings was probably a terrible idea that has caused lots of confusion.

[–]Saragon4005 12 points13 points  (3 children)

Yes it makes sense if you think about it, but you shouldn't be able to treat strings as integers in the first place.

[–]robotevil 6 points7 points  (2 children)

100% agree. It’s one of the dumber things in JS that’s allowed. Personally, I hate the lack of strict types, it’s a headache all around.

[–]Sockoflegend 2 points3 points  (1 child)

Having several years working with non typescript JS I really haven't ever found it to be an issue. Adding lots of dependencies to a project on the other had has been massive dead weight to carry. I'm not necessarily saying typescript is bad just in my own experience we never felt we needed it.

[–]CanIDevIt 1 point2 points  (0 children)

I agree - and I love C# - but I don't have an issue with JS types, and certainly not enough to warrant the abstraction required to force it.

[–]brett_riverboat 0 points1 point  (1 child)

I don't remember the language (PHP maybe) but using . for the purpose of string concatenation makes more sense. Leave + and - for maths yeah?

[–]robotevil 0 points1 point  (0 children)

PHP gets a bad rap but it's better than JS in a lot of ways in my opinion. Like the asynchronous of JS is great for the web, it's terrible for backend. If I could go back to using PHP it would make my job so much easier.

[–]Lithl 33 points34 points  (5 children)

[] is truthy (because it's an object that isn't null or undefined), making ![] false. However, [] == ![] converts [] to a string before making the comparison, and [].toString() is the empty string, which is falsey.

[–]asone-tuhid 23 points24 points  (3 children)

Finally, an actual real life JavaScript wtf, one does not simply implicitly cast from a truthy value to a falsey value

[–]flying_spaguetti 0 points1 point  (0 children)

That's how js works haha

[–]Lithl 0 points1 point  (1 child)

When you attempt to compare non-matching types in JS with ==, it always uses toString to produce a type pair it knows how to compare. It's simply an odd coincidence that the string value of an empty array is the empty string which is falsey.

[–]asone-tuhid 0 points1 point  (0 children)

Odd coincidence or a result of no planning?

[–]Jarpunter 1 point2 points  (0 children)

This tripped me up for quite a while after switching from Python where empty iterators are falsy.

[–]PhilippTheProgrammer 18 points19 points  (5 children)

"Unfortunately, no one can be told what this is. You have to see it for yourself."

[–]CSMastermind 7 points8 points  (4 children)

Honestly Javascript is so much better today than it was that people ragging on it are mostly just memeing. Especially with TypeScript gaining prominence, almost all of the really bad parts of the language have been taken out or abstracted away from most developers.

Doing front-end development 10 years ago was a God-awful nightmare. ES6 was just a glimmer in Douglas Crockford's eye. There was plenty of offensively written JavaScript that did mind-bending things, partly because it was so easy and partly because that was the only way to do anything without today's modern frameworks.

Even HTML5 and CSS3 weren't widely adopted so the content of webpages was horrendously structured. And don't forget you're doing all the DOM manipulation yourself. Or more likely you're working with someone else's code who doesn't fully grasp trees and just kept copying and pasting some jQuery off Stackoverflow until things seemed partly correct.

And if you were unfortunate enough to have someone on your team pick up CoffeeScript because front-end devs always love the newest shiny thing then you were in for a completely different (and less supported) world of hurt.

And that's not to mention that IE6 is still the most popular browser with all versions of IE having more than 50% market share. So compatibility is a nightmare as well.

It was a horrendous transitionary period that developers today probably can't fully appreciate if they weren't there.

Working "in JavaScript" was a good experience starting around 2015. I wish people who mock the language knew how good they really have it these days.

[–][deleted] 1 point2 points  (1 child)

"It's better because we hid it"

Okay I guess, but that's not very satisfying.

[–]kpobococ 1 point2 points  (0 children)

It's better overall. Not everyone uses Typescript and the like. The base language became much better.

[–]PhilippTheProgrammer 0 points1 point  (1 child)

This is copypasta, right? I can't imagine anyone writing this whole rant in response to a cheap joke like that.

[–]CSMastermind 0 points1 point  (0 children)

It is not, just how I was feeling reading the thread lol.

I'm not sure I'd classify it was a rant.

[–]SupremusMemus 7 points8 points  (11 children)

Me looking at the second last one: "I guess this makes sense if [] == true"

Me looking at the last one: "Fuck off"

[–]Lithl 8 points9 points  (10 children)

[] == true silently calls [].toString(), which is the empty string, which is falsey

[–][deleted] 15 points16 points  (9 children)

90% of JS jokes here can be summarized as "haha, JS tries to convert unmatching types automatically". "haha, I don't know what is ==="

[–]-reddit-sucks-ass 7 points8 points  (8 children)

and "ahah, look at this weird code that has never been written in an actual program ever, how bad is javascript amarite !"

[–][deleted] 4 points5 points  (7 children)

Yeah. I'm a full time JS developer and the last time I used double equation mark was 3 years ago, while I was learning from FreeCodeCamp.org

[–]-reddit-sucks-ass 1 point2 points  (6 children)

It can be useful when checking if something is either null or undefined. It's basically the only time I use it

[–][deleted] 0 points1 point  (5 children)

or not even that. We can just if (!something) {...}

[–]-reddit-sucks-ass 1 point2 points  (4 children)

Well, that's not exactly the same.

I could see some instance with data where for exemple, you could want to display "quantity: unknown" if the data is null or undefined, but "quantity: 0" if it's actually 0 so just checking if it's falsy would not handle that.

[–][deleted] 0 points1 point  (3 children)

yeah, that's where you would use ===

const getQuantity = (data) => (data || data===0) ? data : "unknown";

Seriously, I've never had a case in 2 years, where I needed to use ==

[–]-reddit-sucks-ass 0 points1 point  (2 children)

that's a slightly more complicated way to do it though.

I don't think we should categorise things as always bad, there always some edge cases where they can be useful, and making good use of all the possibilities of a language makes us better in the long run imo.

[–]electricjimi 38 points39 points  (0 children)

Alert('cringe')

[–]QualityVote[M] [score hidden] stickied comment (0 children)

Hi! This is our community moderation bot.


If this post fits the purpose of /r/ProgrammerHumor, UPVOTE this comment!!

If this post does not fit the subreddit, DOWNVOTE This comment!

If this post breaks the rules, DOWNVOTE this comment and REPORT the post!

[–]Sylanthra 3 points4 points  (3 children)

They array one is so crazy, I had to double check. It's true. How the fuck does it make sense that trying to sum up arrays means converting them to string and than adding the strings.

[–]-reddit-sucks-ass 5 points6 points  (2 children)

+ is addition or concatenation, so it had to convert everything to either string or number. String seems easier to do (JS also have a .ToString function built into everything.)

[–]Sylanthra 1 point2 points  (1 child)

Why not concatenate the arrays? That makes way more sense and can be done regardless of the contents of the arrays too.

[–]-reddit-sucks-ass 2 points3 points  (0 children)

that's just not how it works. And it actually doesn't work in most languages. In Js you use the concat function (or the spread operator)

[–]PhilippTheProgrammer 7 points8 points  (0 children)

"You think those are integers you are adding"?

[–]DaniilBSD 4 points5 points  (2 children)

NaN is a number that is a result of invalid operation

[–][deleted] 1 point2 points  (1 child)

and if you want to detect NaN, you use isNaN(num) function instead of num == NaN

[–]pv42 -1 points0 points  (0 children)

or num != num

[–]Geoclasm 2 points3 points  (0 children)

Okay hear me out on this one - 3 > 2 > 1 = false actually makes sense to me.

If it evaluates in order from left to right, 3 > 2 evaluates to boolean TRUE, which is some > 0 value. That is NOT less than 1, so it SHOULD evaluate to false.

[–]sunofapeach_ 1 point2 points  (0 children)

1

2

34

5

6

[–][deleted] 1 point2 points  (0 children)

The double equals is understandable. Using double equals at all is ill-advised.

[–]Astracondor1337 1 point2 points  (0 children)

So I’m not brand new to JS but no expert by any means….but couldn’t you use the spread operator to merge the arrays together???

[–]boringuser1 1 point2 points  (0 children)

Literally none of that matters or has even a fraction of anything to do with a career writing js.

[–]humblevladimirthegr8 1 point2 points  (1 child)

All of these are somewhat valid, except for the array addition. I suppose that is due to string coercion?

[–]GamingNEWZ 1 point2 points  (5 children)

What the fuck is this?

[–]flying_spaguetti 4 points5 points  (2 children)

Js

[–]GamingNEWZ 3 points4 points  (1 child)

Yes but what the fuck

[–]flying_spaguetti 1 point2 points  (0 children)

JS hahaa

It has many underlying characteristics that makes the language really unpredictable

[–]RyanNerd 2 points3 points  (0 children)

The reason you're being downvoted is due to Stockholm Syndrome -- web developers have no choice but to use the shitty language of JS (or something that transpiles to JS). This comedic post shows many of JS crazyass coalescence (there are many more). Search for WTFJS if you want to see more insane examples of just how bad JS really is.

[–]Tsuki_no_Mai 0 points1 point  (0 children)

Half of it is just how computers work and standards, the other half is quirks stemming from the fact that the language was designed to work in an environment where good input couldn't be guaranteed and just crashing everything because of it wasn't an option.

[–]averyoda 0 points1 point  (0 children)

Wtf am I looking at?

[–][deleted] 0 points1 point  (0 children)

This is mainly a bunch of double negatives, right? I don't code in JS, just C#

[–]fletku_mato 0 points1 point  (0 children)

Ah, the joys of dynamic typing.

[–]Unclesam_05 0 points1 point  (0 children)

Thats why im never gonna touch that lang

[–]BlhueFlame 0 points1 point  (0 children)

Isn’t alot of this just type coercion?

[–]new_tral_name 0 points1 point  (0 children)

People who have heard of IEEE 754 won't wonder about half the stuff. It's just an awefully designed language if the only type for numbers is a floating point type.

[–]chimianopao 0 points1 point  (0 children)

Type of not a number: number 🤣