News for hackers. Stuff that matters.
Re:JavaScript in 2015 by hfsktr on Wednesday January 07, 2015 @09:49AM (#8850723)
I know what the batman example gives but I don't get how it's a WTF?

I assume the expected output is "wa" but why should a string less an integer produce that?

Re:JavaScript in 2015 by squeaky-clean on Wednesday January 07, 2015 @10:57AM (#8851208)
There is no expected output. It doesn't make sense to subtract the value 1 from the string "wat". But Javascript will cast both of them to numbers, then try to subtract 1 from NaN. However, if you do "wat" + 1, Javascript will cast both of them to strings, and append "1" to "wat".

It's not just the odd behavior, but the inconsistency.

Re:JavaScript in 2015 by hfsktr on Wednesday January 07, 2015 @02:52PM (#8852845)
Ok that makes a bit more sense. I didn't even think about how it would be if you tried to add them.

I like javascript but I don't do anything so complicated (or maybe not the right types of things) that I run into many of these situations.

Re:JavaScript in 2015 by moron4hire on Wednesday January 07, 2015 @10:20AM (#8850916)
No, it produces NaN (Not-a-Number), and then repeats it in a string 16 times. The WTF is that NaN can be concatenated to strings as "NaN".
Re:JavaScript in 2015 by insin on Wednesday January 07, 2015 @10:38AM (#8851064)
NaN is passed to Array.prototype.join() as the separator to join strings with, so it gets coerced to String ("NaN"). It's deliberately working backward from the behaviour of Array.prototype.join() to create a contrived example for giggles, not a WTF.

It's no more a WTF than this, which follows the same principle: https://gist.github.com/insin/1183916

Re:JavaScript in 2015 by moron4hire on Wednesday January 07, 2015 @11:00AM (#8851230)
Oh, I understand that. I was more trying to explain why someone might think it was a WTF.

And I'm pretty sure your example returns the decimal 15. Comma operator returns the last element in the list, and parseInt will truncate strings with non-number-like text to the number-like part. Here, the number-like part is a hexadecimal code, triggering another feature of parseInt that it can figure out the radix on the fly.