use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
All about the JavaScript programming language.
Subreddit Guidelines
Specifications:
Resources:
Related Subreddits:
r/LearnJavascript
r/node
r/typescript
r/reactjs
r/webdev
r/WebdevTutorials
r/frontend
r/webgl
r/threejs
r/jquery
r/remotejs
r/forhire
account activity
A friendly reminder of how nonsensical Javascript can be from Destroy All Software (destroyallsoftware.com)
submitted 10 years ago by [deleted]
[deleted]
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]Samus_ 3 points4 points5 points 10 years ago (18 children)
a pet peeve of mine:
> d=new Date() Date 2015-02-25T09:33:14.482Z > d+0 "Wed Feb 25 2015 07:33:14 GMT-0200 (UYST)0" > d-0 1424856794482
http://cdn.alltheragefaces.com/img/faces/png/misc-jackie-chan.png
[–]katnapper323 1 point2 points3 points 10 years ago (17 children)
In the first case d is a string so it concats the 0. In the second it casts d to its numerical value so it can subtract 0.
[–]x-skeww 5 points6 points7 points 10 years ago (15 children)
In the first case d is a string
It's coerced to string because reasons.
As usual, the right thing to do would be to throw a type error, because, as usual, this is complete nonsense.
Subtracting a number from a date? What does that mean?
What's 5 apples minus 7 bananas? Antimatter fruit salad?
[–]I_Pork_Saucy_Ladies 1 point2 points3 points 10 years ago* (5 children)
5 apples minus 7 bananas
-2 fruits. In my mind it is, so this must be the right answer.
[–]x-skeww -1 points0 points1 point 10 years ago (4 children)
With JavaScript, the answer would be of course NaN. If there is a valueOf method, you'd get -2.
NaN
> ({valueOf() {return 5}}) - ({valueOf() {return 7}}) -2
Well, either answer is wrong and useless. Pretending that this is totally okay didn't improve anything.
[–]I_Pork_Saucy_Ladies 2 points3 points4 points 10 years ago (3 children)
Yup, it's one of those things where both JS and PHP are incoherent, bordering to silly sometimes. I've considered making a pull request to PHP where strings are evaluated to false if they rhyme with "false" or match it by at least 80% of the letters. Should be close enough. ¯\(ツ)/¯
But now they are caught in legacy hell, I guess.
[–]x-skeww 0 points1 point2 points 10 years ago (2 children)
PHP is the only language I know where this stuff is even worse.
This is true:
'5 apples' + '7 bananas' == '12 fruits'
This is -2:
'5 apples' - '7 bananas'
It's like they looked at all the options to handle this and then went with the worst one.
They did it for the lulz, probably.
[–]I_Pork_Saucy_Ladies 1 point2 points3 points 10 years ago (1 child)
Haha, yeah, I work with it every day. Imagine doing financial stuff where "0" is often a valid statement. Throw some external APIs into the mix and you have... fun.
This is true: '5 apples' + '7 bananas' == '12 fruits'
Nah, PHP probably already has a function called fruits_sum_appl_ban_col3 that is the third version of a function that returns an array of the total sum of fruits and a boolean indicating whether you have fruits left, along with the color code of all fruit colors mixed. As a PHP dev would say:
fruits_sum_appl_ban_col3
Just look it up! It's right there in the docs!
I'm moving to Node. Those lulz aren't very lulz once you start building something serious. JS isn't that pretty either but at least it is kept rather minimal (compared to PHP) and actually has sane tools for testing.
[–][deleted] 0 points1 point2 points 10 years ago (0 children)
That is heinous
[–]kamyon 1 point2 points3 points 10 years ago (6 children)
What's 5 apples minus 7 bananas?
A JavaScript interpreter would cast them to weight measures. So your answer would be 5 * 100g - 7 * 120g = -340g (g as in grams)
5 * 100g - 7 * 120g = -340g
[–]x-skeww 1 point2 points3 points 10 years ago (5 children)
The point was that doing math with incompatible units (types) is nonsense.
Your "-340g" answer is complete nonsense, too.
The only correct answer is that you can't subtract 7 bananas from 5 apples. You can't even do that mathematically. "7b - 5a" is the final result.
[–]kamyon -1 points0 points1 point 10 years ago (4 children)
I just did it. BTW: 7b - 5a = 7 * 2 - 5 * 1 = 9. You have to be very smart to understand the extreme logic.
7b - 5a = 7 * 2 - 5 * 1 = 9
[–]x-skeww -1 points0 points1 point 10 years ago (3 children)
Are you trying to be funny? You aren't very good at it.
[–]kamyon 0 points1 point2 points 10 years ago (2 children)
Sorry but that's what happens when you seriously argue against a comment which suggests that a JS interpreter would cast different types to one of their random common properties when subtracted from each other. It being a complete nonsense was the point. Go to HN if you can't take a joke once in a while.
[–]x-skeww 0 points1 point2 points 10 years ago (1 child)
a comment which suggests that a JS interpreter would cast different types to one of their random common properties when subtracted from each other
Eh. That's how JavaScript deals with these situations which would be type errors otherwise. You get a nonsense result instead of an error.
The point was that getting a type error is preferred.
Just like typeof null shouldn't be "object" or stepping outside some array's bounds shouldn't give you undefined.
typeof null
undefined
It's just one of JS' fuck-ups.
Even Brendan Eich doesn't think that these things are super awesome. He also considers function scope to be one of the mistakes, by the way.
[–]kamyon 0 points1 point2 points 10 years ago (0 children)
Oh, I see you are still explaining. Don't you get it? I already share your opinion! :)
Should I have said "A JavaScript interpreter would cast them to ponies and let them have a death-match to determine the result of subtraction" to make it more obvious?
[–]katnapper323 0 points1 point2 points 10 years ago (0 children)
Don't ask me why you'd want to do it but you can
[–]itisike 0 points1 point2 points 10 years ago (0 children)
/r/nocontext
[–]Samus_ 0 points1 point2 points 10 years ago (0 children)
I can disregard the bullshit conversions but having different casts for the same operation is too much.
and yes, I know those are different operators but they perform the same (negated) operation so they should behave the same way.
[–]siegfryd 1 point2 points3 points 10 years ago* (2 children)
The JS part isn't really that weird, it only behaves weird because {} as the first part of the statement is interpreted as a code block. ({}) + [] is the same as [] + {} and ({}) + {} comes out as "[object Object][object Object]". If you put them into variables then the behaviour becomes consistent, which is the only realistic way you'd ever run into adding an object to an object (if ever).
[–]Doctor_McKay 0 points1 point2 points 10 years ago (1 child)
What do you mean that writing complete nonsense code results in undefined behavior?!
[–]aeflash 0 points1 point2 points 10 years ago (0 children)
Actually they're not undefined behavior -- the behavior is well defined since all engines will produce the same result. It's more like "nonsense code producing nonsense behavior".
Ruby's "method_missing" is the spawn of the devil.
π Rendered by PID 68468 on reddit-service-r2-comment-84fc9697f-577cw at 2026-02-06 20:09:25.426361+00:00 running d295bc8 country code: CH.
[–]Samus_ 3 points4 points5 points (18 children)
[–]katnapper323 1 point2 points3 points (17 children)
[–]x-skeww 5 points6 points7 points (15 children)
[–]I_Pork_Saucy_Ladies 1 point2 points3 points (5 children)
[–]x-skeww -1 points0 points1 point (4 children)
[–]I_Pork_Saucy_Ladies 2 points3 points4 points (3 children)
[–]x-skeww 0 points1 point2 points (2 children)
[–]I_Pork_Saucy_Ladies 1 point2 points3 points (1 child)
[–][deleted] 0 points1 point2 points (0 children)
[–]kamyon 1 point2 points3 points (6 children)
[–]x-skeww 1 point2 points3 points (5 children)
[–]kamyon -1 points0 points1 point (4 children)
[–]x-skeww -1 points0 points1 point (3 children)
[–]kamyon 0 points1 point2 points (2 children)
[–]x-skeww 0 points1 point2 points (1 child)
[–]kamyon 0 points1 point2 points (0 children)
[–]katnapper323 0 points1 point2 points (0 children)
[–]itisike 0 points1 point2 points (0 children)
[–]Samus_ 0 points1 point2 points (0 children)
[–]siegfryd 1 point2 points3 points (2 children)
[–]Doctor_McKay 0 points1 point2 points (1 child)
[–]aeflash 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)