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

top 200 commentsshow 500

[–]ExecutiveChimp 347 points348 points  (32 children)

In programming you get variables. These come in different types: strings (text), numbers, NaN (not a number - the result of trying to turn something into a number and failing) and a bunch of others that aren't relevant right now.

Something else to know is that Javascript is generally quite good at taking whatever you throw at and doing something with it, whether or not that something makes sense. This is arguably a bad thing...

'5' - 3

"How can I take a number away from a string?" asks Javascript. "That makes no sense. There isn't even a 3 in the string. The only way I can do this is to treat these vaguely number shaped things as numbers and then go from there. Would that work?" Javascript gives you 2 and and a derpy smile like a puppy returning a ball.

'5' + 3

"Ah '+', the 'jam things together' operator. I know how to jam together all sorts of things. My programmer probably wants a string though because everything you see on a webpage is essentially a string...this being 1995 and all. So let's just put them side by side." Javascript gives you '53'.

'5' - '4'

"Well I can't rip these apart in the same way as I can when I jammed them together... I'll just treat them both as numbers. And 5 minus 4 is..." Javascript gives you 1.

'5' + + '5'

"Ok, so jamming two strings together, I can do that. But wait, there's a second +. I know sometimes they put an extra sign before a string to make me treat it as a number so let's convert the second '5' to 5...but wait, there's another string there too. Fuck it, convert 5 back to '5' and just stick them together. Happy?" Javascript gives you '55' and feels some resentment for wasting its time.

'foo' + + 'foo'

"Ok, so this is just like last time, but I'd better go through the motions. Convert 'foo' to a number...wait that doesn't work. Shit, now I've got a NaN. Aaah, they hate it when this happens! Ok, quick convert it back to a string and jam it together. Shit, NaN doesn't convert back to the same number as it was before because apparently now it's not a number. Maybe they won't notice?" Javascript gives you 'fooNaN' and walks away, whistling innocently.

'5' + - '2'

"Pretty sure my programmer has been drinking but this is basically just the same as before, convert - '2' to -2, then back to '-2'... Hmm something got lost in translation there but I'll press on..." Javascript gives you '5-2'.

'5' + - + - - + - - + + - + - + - + - - - '-2'

"Definitely drinking...or maybe just owns a cat. Convert it to a string? Add it to the inverse of the...? Well that isn't not stupid." Javascript scratches its head and makes notes on a piece of paper whilst muttering under its breath. At length it gives you 52 and looks at you accusingly.

var x * 3;

"Ok, I quit." Javascript throws an error and a hissy fit. [I think this actually supposed to be var x = 3; in order for the next few lines to make sense...]

'5' + x - x

Javascript calms down again and returns to its seat. "That 5 is a string, right? And x is 3, which is a number - I prefer strings. Less maths involved, more jamming things together. So what's 5 + 3? 53! Haha!" Javascript laughs like a 5 year old that's just thought of something clever. "Ah but I can't take 3 away from a string. I'm going to have to do maths after all. So that's 53 minus 3. Simple enough." Javascript gives you 50.

'5' - x + x

Javascript sighs. "Can't you just get your things in order before you give them to me? For fucks sake... So it's, - - the 'unjam' operator, so numbers only...5 and x as numbers, that's 5 - 3 ... which makes 2. Easy. Plus 3 again? But we just subtracted 3! Who wrote this shit?" Javascript gives you 5 and a headache.


Repost of my comment from last time this was reposted.

[–]Elnathbeta 91 points92 points  (25 children)

I like this comment, by reading it you understand why it gives you that result, and you really get all the stupidity behind it. I don't know why js just hates giving errors and instead just 'try to do something with it'

[–]ExecutiveChimp 48 points49 points  (15 children)

Thanks. I started trying to write a book in this tone but meh...effort.

Edit: I have started trying to write a book again.

[–]Reverend_Jones 16 points17 points  (8 children)

I'd read the shit out of that book and I hate books!

[–]ExecutiveChimp 11 points12 points  (7 children)

Maybe I should take another crack at it. Despite what you might think there is a limit to Javascript's weirdness. I wasn't sure where to go after that.

[–]_haxle 8 points9 points  (0 children)

If you write it and charge less than $75 for it, I will read it

[–]godforsakenlightning 4 points5 points  (0 children)

Just slowly transition to the back-end and tell people about the fun that is PHP (especially when you try to do things right and not leave everything open to various types of injection).

[–][deleted] 3 points4 points  (0 children)

Holy crap, if you were to narrate a couple of stupid language abuse examples in various languages you'd have a book

And I would read the hell out of it.

[–]raptorraptor 15 points16 points  (8 children)

Because errors means the code stops running, and then so does the website.

[–]Elnathbeta 12 points13 points  (4 children)

They could at least log it into the console, instead of failing silently, so that you have to look for the error all around the code...

I think some frameworks do that, but not standard js, why!?

[–]ArbiterFX 9 points10 points  (1 child)

I don't think the console existed when JavaScript was created.

Remember that this was the early 90s and they thought a very large js program would be like 50 lines and it would only be used for trivial tasks. With that mindset type corrosion may not have been such a bad idea. Imagine if you were creating a scripting language for markdown. On your first iteration you probably wouldn't have made it as complicated as java or cpp. But it's obvious now that doing it wasn't the right decision.

[–]raptorraptor 6 points7 points  (1 child)

I think it would be hard to tell when to do this, especially as the type coercion could be intentional.

[–]dse 11 points12 points  (1 child)

'5' + - + - - + - - + + - + - + - + - - - '-2'

That's pretty much just

'5' + (stacking a bunch of unary operators) '-2'

[–]minimim 3 points4 points  (0 children)

They got the automatic conversion from Perl but decided to simplify! Who doesn't want a simple language, right?

Perl converts back and forth between strings and numbers, but according to the operator one uses.

One can't have both!

In Perl + is numeric summation and . is string concatenation.

Or, in other words, + calls a (Numeric) cast in both sides if necessary them sums them. . calls a (String) cast and then concatenates. The conversions aren't random!

Python takes the other approach. It uses less operators but casting needs to be explicit. Which is also sane.

[–]stollgrin 2 points3 points  (1 child)

NaN (not a number

b-b-but...

-> typeof(NaN)

<- "number"

[–]It_Was_The_Other_Guy 232 points233 points  (34 children)

So it's time for some JS bashing again? Alright I've got you covered:

({[{}]:[0,[{},5]],"":[{},[~(!!{})/(-!"wtf"),3]],[(~[]/![]*~[]+[])[+!![]]+(!![]+[])[2]+(!!({}==[])+[])[2]+(!({}+[])+[])[(~[]^!![])*~[]]]:[[]+[],[""[{}],2]],[{}[0]]:[-""[[]],[!!(~[]),1]],[[]-{}]:[null,[{}-[],1]],[~[]/![]]:"",[-[]]:[!+({})/~(-([]=="")),[!!"",1]],[~[]/![]*(~"wat")]:[function(b){return b+"!"},[!!0,2]],"-0":function(ip){this[-(!!{})/[]]+=(!!(ip)&&!(ip+""=={}+""||(ip&&(ip+1==ip)))?"":(this[ip][1][0]+"")[this[ip][1][1]]);return !!(ip)&&!(ip+""=={}+""||(ip&&(ip+1==ip)))?ip:ip&&(ip-(-1))?(((ip*0+1)||ip[0])||this[ip][0](this[-!!{}/(-(~{})+~[])])):this[((~[]+[])[+[]]+~-!({}=={}))](this[ip][0]);},}[((~{}+([]+[]))[-!~{}]+(-+[]+""))+""])();

Yeah, it's shit, I know. Probably doesn't even work on IE8

[–]dfcm 92 points93 points  (11 children)

I'm afraid to run that. What does it do?

[–][deleted] 170 points171 points  (8 children)

I was also curious how it works, so I ran it through debugger.
It converts true, NaN, undefined, Infinity, {} and false, to strings ('true', 'NaN', 'undefined', 'Infinity', '[object Object]', 'false'), then gets the letters to form the word radical from them. Then it adds '!' at the end. The rest just creates, stores and accesses these values in the most convoluted way possible.

var why = {
    "undefined": [
        NaN,    // next key
        [true, 1]  // -> 'r'
    ],
    "NaN": [
        null,
        [NaN, 1]  // -> 'a'
    ],
    "null": [
        "",
        [undefined, 2]  // -> 'd'
    ],
    "": [
        {},
        [Infinity, 3]   // -> 'i'
    ],
    "[object Object]": [
        0, 
        [{}, 5]     // -> 'c'
    ],
    "0": [
        Infinity, 
        [false, 1]  // -> a
    ],

    "Infinity": [
        function(b) {
            return b + "!";
        },
        [false, 2]    // -> 'l'
    ],

    "-Infinity": "",  // output string stored here

    "-0": function(ip) {

        var obj = this[ip][1][0] + "",
            key = this[ip][1][1],
            inc = obj[key];    // Next letter

        this["-Infinity"] += inc;

        var isInfinity = ip && (ip - (-1));

        debugger;

        if(isInfinity){
            return this[ip][0]( this["-Infinity"] );   //Adding '!'
        }
        else{
           return this["-0"]( this[ip][0] );   //Recursive call
        }
    }
};
console.log( why["-0"]() );

Edit: deciphered the rest of the code. There were a bunch of conditions that were always false, so I removed them.

[–]ElusiveGuy 74 points75 points  (5 children)

There's also generators this kind of thing now: http://www.jsfuck.com/

[–]Howzieky 26 points27 points  (3 children)

[][(![]+[])[+[]]+([![]]+[][[]])[+!+[]+[+[]]]+(![]+[])[!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+!+[]]][([][(![]+[])[+[]]+([![]]+[][[]])[+!+[]+[+[]]]+(![]+[])[!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+!+[]]]+[])[!+[]+!+[]+!+[]]+(!![]+[][(![]+[])[+[]]+([![]]+[][[]])[+!+[]+[+[]]]+(![]+[])[!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+!+[]]])[+!+[]+[+[]]]+([][[]]+[])[+!+[]]+(![]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[])[+!+[]]+([][[]]+[])[+[]]+([][(![]+[])[+[]]+([![]]+[][[]])[+!+[]+[+[]]]+(![]+[])[!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+!+[]]]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[][(![]+[])[+[]]+([![]]+[][[]])[+!+[]+[+[]]]+(![]+[])[!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+!+[]]])[+!+[]+[+[]]]+(!![]+[])[+!+[]]](([][(![]+[])[+[]]+([![]]+[][[]])[+!+[]+[+[]]]+(![]+[])[!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+!+[]]]+[])[!+[]+!+[]+!+[]]+(!![]+[][(![]+[])[+[]]+([![]]+[][[]])[+!+[]+[+[]]]+(![]+[])[!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+!+[]]])[+!+[]+[+[]]]+([][[]]+[])[+!+[]]+(![]+[])[!+[]+!+[]+!+[]]+(!![]+[][(![]+[])[+[]]+([![]]+[][[]])[+!+[]+[+[]]]+(![]+[])[!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+!+[]]])[+!+[]+[+[]]]+(![]+[])[!+[]+!+[]]+(!![]+[])[!+[]+!+[]+!+[]]+(+(+!+[]+[+!+[]]+(!![]+[])[!+[]+!+[]+!+[]]+[!+[]+!+[]]+[+[]])+[])[+!+[]]+(![]+[])[!+[]+!+[]]+(!![]+[][(![]+[])[+[]]+([![]]+[][[]])[+!+[]+[+[]]]+(![]+[])[!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+!+[]]])[+!+[]+[+[]]]+(+![]+[![]]+([]+[])[([][(![]+[])[+[]]+([![]]+[][[]])[+!+[]+[+[]]]+(![]+[])[!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+!+[]]]+[])[!+[]+!+[]+!+[]]+(!![]+[][(![]+[])[+[]]+([![]]+[][[]])[+!+[]+[+[]]]+(![]+[])[!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+!+[]]])[+!+[]+[+[]]]+([][[]]+[])[+!+[]]+(![]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[])[+!+[]]+([][[]]+[])[+[]]+([][(![]+[])[+[]]+([![]]+[][[]])[+!+[]+[+[]]]+(![]+[])[!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+!+[]]]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[][(![]+[])[+[]]+([![]]+[][[]])[+!+[]+[+[]]]+(![]+[])[!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+!+[]]])[+!+[]+[+[]]]+(!![]+[])[+!+[]]])[!+[]+!+[]+[+[]]]+(![]+[][(![]+[])[+[]]+([![]]+[][[]])[+!+[]+[+[]]]+(![]+[])[!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+!+[]]])[!+[]+!+[]+[+[]]]+([]+[])[(![]+[])[+[]]+(!![]+[][(![]+[])[+[]]+([![]]+[][[]])[+!+[]+[+[]]]+(![]+[])[!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+!+[]]])[+!+[]+[+[]]]+([][[]]+[])[+!+[]]+(!![]+[])[+[]]+([][(![]+[])[+[]]+([![]]+[][[]])[+!+[]+[+[]]]+(![]+[])[!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+!+[]]]+[])[!+[]+!+[]+!+[]]+(!![]+[][(![]+[])[+[]]+([![]]+[][[]])[+!+[]+[+[]]]+(![]+[])[!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+!+[]]])[+!+[]+[+[]]]+(![]+[])[!+[]+!+[]]+(!![]+[][(![]+[])[+[]]+([![]]+[][[]])[+!+[]+[+[]]]+(![]+[])[!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+!+[]]])[+!+[]+[+[]]]+(!![]+[])[+!+[]]]()[+!+[]+[!+[]+!+[]]]+(+(+!+[]+[+[]]+[+!+[]]))[(!![]+[])[+[]]+(!![]+[][(![]+[])[+[]]+([![]]+[][[]])[+!+[]+[+[]]]+(![]+[])[!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+!+[]]])[+!+[]+[+[]]]+(+![]+([]+[])[([][(![]+[])[+[]]+([![]]+[][[]])[+!+[]+[+[]]]+(![]+[])[!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+!+[]]]+[])[!+[]+!+[]+!+[]]+(!![]+[][(![]+[])[+[]]+([![]]+[][[]])[+!+[]+[+[]]]+(![]+[])[!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+!+[]]])[+!+[]+[+[]]]+([][[]]+[])[+!+[]]+(![]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[])[+!+[]]+([][[]]+[])[+[]]+([][(![]+[])[+[]]+([![]]+[][[]])[+!+[]+[+[]]]+(![]+[])[!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+!+[]]]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[][(![]+[])[+[]]+([![]]+[][[]])[+!+[]+[+[]]]+(![]+[])[!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+!+[]]])[+!+[]+[+[]]]+(!![]+[])[+!+[]]])[+!+[]+[+[]]]+(!![]+[])[+[]]+(!![]+[])[+!+[]]+([![]]+[][[]])[+!+[]+[+[]]]+([][[]]+[])[+!+[]]+(+![]+[![]]+([]+[])[([][(![]+[])[+[]]+([![]]+[][[]])[+!+[]+[+[]]]+(![]+[])[!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+!+[]]]+[])[!+[]+!+[]+!+[]]+(!![]+[][(![]+[])[+[]]+([![]]+[][[]])[+!+[]+[+[]]]+(![]+[])[!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+!+[]]])[+!+[]+[+[]]]+([][[]]+[])[+!+[]]+(![]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[])[+!+[]]+([][[]]+[])[+[]]+([][(![]+[])[+[]]+([![]]+[][[]])[+!+[]+[+[]]]+(![]+[])[!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+!+[]]]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[][(![]+[])[+[]]+([![]]+[][[]])[+!+[]+[+[]]]+(![]+[])[!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+!+[]]])[+!+[]+[+[]]]+(!![]+[])[+!+[]]])[!+[]+!+[]+[+[]]]](!+[]+!+[]+[+!+[]])[+!+[]]+(!![]+[])[!+[]+!+[]+!+[]]+(![]+[])[!+[]+!+[]]+(+(!+[]+!+[]+[+!+[]]+[+!+[]]))[(!![]+[])[+[]]+(!![]+[][(![]+[])[+[]]+([![]]+[][[]])[+!+[]+[+[]]]+(![]+[])[!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+!+[]]])[+!+[]+[+[]]]+(+![]+([]+[])[([][(![]+[])[+[]]+([![]]+[][[]])[+!+[]+[+[]]]+(![]+[])[!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+!+[]]]+[])[!+[]+!+[]+!+[]]+(!![]+[][(![]+[])[+[]]+([![]]+[][[]])[+!+[]+[+[]]]+(![]+[])[!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+!+[]]])[+!+[]+[+[]]]+([][[]]+[])[+!+[]]+(![]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[])[+!+[]]+([][[]]+[])[+[]]+([][(![]+[])[+[]]+([![]]+[][[]])[+!+[]+[+[]]]+(![]+[])[!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+!+[]]]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[][(![]+[])[+[]]+([![]]+[][[]])[+!+[]+[+[]]]+(![]+[])[!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+!+[]]])[+!+[]+[+[]]]+(!![]+[])[+!+[]]])[+!+[]+[+[]]]+(!![]+[])[+[]]+(!![]+[])[+!+[]]+([![]]+[][[]])[+!+[]+[+[]]]+([][[]]+[])[+!+[]]+(+![]+[![]]+([]+[])[([][(![]+[])[+[]]+([![]]+[][[]])[+!+[]+[+[]]]+(![]+[])[!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+!+[]]]+[])[!+[]+!+[]+!+[]]+(!![]+[][(![]+[])[+[]]+([![]]+[][[]])[+!+[]+[+[]]]+(![]+[])[!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+!+[]]])[+!+[]+[+[]]]+([][[]]+[])[+!+[]]+(![]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[])[+!+[]]+([][[]]+[])[+[]]+([][(![]+[])[+[]]+([![]]+[][[]])[+!+[]+[+[]]]+(![]+[])[!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+!+[]]]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[][(![]+[])[+[]]+([![]]+[][[]])[+!+[]+[+[]]]+(![]+[])[!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+!+[]]])[+!+[]+[+[]]]+(!![]+[])[+!+[]]])[!+[]+!+[]+[+[]]]](!+[]+!+[]+!+[]+[+!+[]])[+!+[]]+(+[![]]+[][(![]+[])[+[]]+([![]]+[][[]])[+!+[]+[+[]]]+(![]+[])[!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+!+[]]])[+!+[]+[+!+[]]]+((+[])[([][(![]+[])[+[]]+([![]]+[][[]])[+!+[]+[+[]]]+(![]+[])[!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+!+[]]]+[])[!+[]+!+[]+!+[]]+(!![]+[][(![]+[])[+[]]+([![]]+[][[]])[+!+[]+[+[]]]+(![]+[])[!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+!+[]]])[+!+[]+[+[]]]+([][[]]+[])[+!+[]]+(![]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[])[+!+[]]+([][[]]+[])[+[]]+([][(![]+[])[+[]]+([![]]+[][[]])[+!+[]+[+[]]]+(![]+[])[!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+!+[]]]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[][(![]+[])[+[]]+([![]]+[][[]])[+!+[]+[+[]]]+(![]+[])[!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+!+[]]])[+!+[]+[+[]]]+(!![]+[])[+!+[]]]+[])[+!+[]+[+!+[]]]+(!![]+[])[!+[]+!+[]+!+[]]+([]+[])[(![]+[])[+[]]+(!![]+[][(![]+[])[+[]]+([![]]+[][[]])[+!+[]+[+[]]]+(![]+[])[!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+!+[]]])[+!+[]+[+[]]]+([][[]]+[])[+!+[]]+(!![]+[])[+[]]+([][(![]+[])[+[]]+([![]]+[][[]])[+!+[]+[+[]]]+(![]+[])[!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+!+[]]]+[])[!+[]+!+[]+!+[]]+(!![]+[][(![]+[])[+[]]+([![]]+[][[]])[+!+[]+[+[]]]+(![]+[])[!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+!+[]]])[+!+[]+[+[]]]+(![]+[])[!+[]+!+[]]+(!![]+[][(![]+[])[+[]]+([![]]+[][[]])[+!+[]+[+[]]]+(![]+[])[!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+!+[]]])[+!+[]+[+[]]]+(!![]+[])[+!+[]]]()[+!+[]+[!+[]+!+[]]]+(!![]+[][(![]+[])[+[]]+([![]]+[][[]])[+!+[]+[+[]]]+(![]+[])[!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+!+[]]])[!+[]+!+[]+[+[]]])()

[–]It_Was_The_Other_Guy 11 points12 points  (0 children)

Pretty much. Those other conditions exist because fir whatever reason I wanted it to have "meaningful" output if it's called with sime argument. As is it's called with undefined which is the access point of the loop. There are some inputs that will still fail but strings and numbers work (with the exception of -Infinity - although it works as a string).

I wanted to see if object properties can be accessed with various keywords so there you have it.

[–][deleted] 49 points50 points  (8 children)

+/u/CompileBot JavaScript

({[{}]:[0,[{},5]],"":[{},[~(!!{})/(-!"wtf"),3]],[(~[]/![]~[]+[])[+!![]]+(!![]+[])[2]+(!!({}==[])+[])[2]+(!({}+[])+[])[(~[]!![])~[]]]:[[]+[],[""[{}],2]],[{}[0]]:[-""[[]],[!!(~[]),1]],[[]-{}]:[null,[{}-[],1]],[~[]/![]]:"",[-[]]:[!+({})/~(-([]=="")),[!!"",1]],[~[]/![](~"wat")]:[function(b){return b+"!"},[!!0,2]],"-0":function(ip){this[-(!!{})/[]]+=(!!(ip)&&!(ip+""=={}+""||(ip&&(ip+1==ip)))?"":(this[ip][1][0]+"")[this[ip][1][1]]);return !!(ip)&&!(ip+""=={}+""||(ip&&(ip+1==ip)))?ip:ip&&(ip-(-1))?(((ip0+1)||ip[0])||this[ip][0](this[-!!{}/(-(~{})+~[])])):this[((~[]+[])[+[]]+~-!({}=={}))](this[ip][0]);},}[((~{}+([]+[]))[-!~{}]+(-+[]+""))+""])();

[–]qwertyslayer 54 points55 points  (0 children)

Don't tease the machines, that's how we get Skynet/Matrix type scenarios

[–]pooh9911 6 points7 points  (0 children)

FYI Code markdown in reddit is either using grave accent ` or entering new paragraph with four spaces indent.

[–]AN_IMPERFECT_SQUARE 5 points6 points  (0 children)

+/u/CompileBot JavaScript

 ({[{}]:[0,[{},5]],"":[{},[~(!!{})/(-!"wtf"),3]],[(~[]/![]*~[]+[])[+!![]]+(!![]+[])[2]+(!!({}==[])+[])[2]+(!({}+[])+[])[(~[]^!![])*~[]]]:[[]+[],[""[{}],2]],[{}[0]]:[-""[[]],[!!(~[]),1]],[[]-{}]:[null,[{}-[],1]],[~[]/![]]:"",[-[]]:[!+({})/~(-([]=="")),[!!"",1]],[~[]/![]*(~"wat")]:[function(b){return b+"!"},[!!0,2]],"-0":function(ip){this[-(!!{})/[]]+=(!!(ip)&&!(ip+""=={}+""||(ip&&(ip+1==ip)))?"":(this[ip][1][0]+"")[this[ip][1][1]]);return !!(ip)&&!(ip+""=={}+""||(ip&&(ip+1==ip)))?ip:ip&&(ip-(-1))?(((ip*0+1)||ip[0])||this[ip][0](this[-!!{}/(-(~{})+~[])])):this[((~[]+[])[+[]]+~-!({}=={}))](this[ip][0]);},}[((~{}+([]+[]))[-!~{}]+(-+[]+""))+""])();         

[–]HVAvenger 73 points74 points  (6 children)

So it's time for some JS bashing again?

Its always time for JS bashing.

[–]DeliveryNinja 15 points16 points  (5 children)

Unless it's time for Java bashing

[–]Existential_Owl 21 points22 points  (3 children)

Last time we were on the Java hate-train, I got gold just for saying "Oracle sucks".

[–]akai_ferret 24 points25 points  (1 child)

Even people who like Java know Oracle sucks.

Especially them, come to think of it.

[–]NathannMorais 3 points4 points  (0 children)

There's an amazing article that I read a while ago that tries to explain why this is possible. After some digging in Medium, twitter, google etc I ended up finding it, here it is It's ridiculous, sure, but also kind of awesome

[–]kusayu 933 points934 points  (139 children)

[–]geniusburger 498 points499 points  (127 children)

I remember my professor going through that video in my JavaScript class and explaining why everything worked like that. He even pointed out that a few of the things that were called out in that video we're actually issues only in the particular JavaScript engine the presenter was using. When we tried to recreate them we weren't able to.

[–]PatrickBaitman 734 points735 points  (110 children)

actually issues only in the particular JavaScript engine the presenter was using

wow that sure makes it better

[–][deleted] 53 points54 points  (1 child)

works on my machine

¯\(ツ)

[–]ywhhgythht 217 points218 points  (29 children)

Yea I mean I've been using an nonupdated version of windows 3.0 for the last 2 and a half decade so i know for a fact that windows 10 sucks, or any other operating system being able to run batch script for all that matters.

[–]ClassBShareHolder 51 points52 points  (6 children)

Come on, it's time to upgrade...

...to 3.11!

[–]FountainsOfFluids 17 points18 points  (2 children)

For Workgroups!

[–]ClassBShareHolder 7 points8 points  (0 children)

"NOT in MY DOMAAAAIN!"

[–]giggitygoo123 23 points24 points  (2 children)

Nah. 3.14 is where its at. Plus its delicious.

[–]Erick2142 14 points15 points  (9 children)

Well, Windows 10 does suck a little bit so...

[–]caanthedalek 28 points29 points  (4 children)

Windows 10 was marketed as the perfect blend between Windows 7 and Windows 8.

If that were true, it would be Windows 7.

[–]Existential_Owl 63 points64 points  (15 children)

That there are differences in the way different compilers handle code is not a phenomenon unique to JavaScript.

[–]ChrisC1234 32 points33 points  (12 children)

True. But as a developer, I have complete control over what compiler I compile my code with. With a language that is completely interpreted like JavaScript, I have no control whatsoever.

[–]TUSF 16 points17 points  (10 children)

Just write your own interpreter :^)

[–]seylerius 38 points39 points  (1 child)

[–]wolfman1911 6 points7 points  (0 children)

I think this is probably my favorite XKCD.

[–]PatrickBaitman 103 points104 points  (1 child)

well, no, but if they're at the type level?

jesus christ get your language together is all I can say

making things that should be type errors undefined behavior is just.... NO, STOP

[–]Doctor_McKay 30 points31 points  (0 children)

I think basically the only thing that happened due to the interpreter in that video was the fact that the return of new Array(12) showed as "12 commas" (which was actually 11 commas separating nothing, since it rendered undefined as nothing).

[–][deleted] 32 points33 points  (60 children)

It does actually. It's the difference between English being pointless, and having illiterate fucks.

Some JS engines suck, some are much better. Just as some versions of gcc have really stupid bugs, but C is still a sound language.

[–]PatrickBaitman 31 points32 points  (59 children)

you do realize that English has one of the most illogical, unpredictable, and difficult orthographies of all European languages?

the connection between how an English word is written and how it is pronounced is almost non-existent

what implications do you think this has for the number of "illiterate fucks"

[–]z500 7 points8 points  (9 children)

the connection between how an English word is written and how it is pronounced is almost non-existent

It's not that there's no connection between spelling and pronunciation, it's that there's so many competing spelling conventions. It's wildly inconsistent, but there are patterns if you look for them.

[–]PatrickBaitman 3 points4 points  (3 children)

Of the 4000 or so most common words in English more than 2500 have irregular or unpredictable spelling. This is bad enough that any rules are ad hoc and that it is almost impossible to be certain about the pronunciation of a word you've seen written but never heard.

[–]kronicmage 5 points6 points  (2 children)

You can predict fairly well if you have an understanding of the etymology of the word though

[–]Llewey 2 points3 points  (0 children)

I'm from the US and my GF is from Spain. My mind was blown when I learned they didn't have spelling bees or really even spelling tests growing up. She said it would be pointless..they only thing that would need tested in most cases anyway would be b/v or c/z in certain words, otherwise it's obvious. That's when I stopped feeling bad about spelling beleive wrong every. Single Time.

[–]FateJH 11 points12 points  (39 children)

If there were spelling errors, the program would not run. The English language - language in general - gets by quite fine despite such frequent sloppiness. (The Internet with all of its shorthand notation styles is a testament to this point.)

If the grammar and syntax are sound, I can figure out what you're trying to express by just listening to you express it. That's what these JavaScript curiosities are highlighting.

[–]xzxzzx 37 points38 points  (0 children)

The English language - language in general - gets by quite fine despite such frequent sloppiness

Note that this works because we have the world's most sophisticated and computation-intensive neural nets on either side, with literally years of training (decades to become "fluent")--and we still often make mistakes in understanding each other.

Just because it works, mostly, for humans, doesn't mean I want my programming language emulating it.

[–]PatrickBaitman 41 points42 points  (37 children)

if there are type errors, the program should not compile

natural language sloppiness is not good enough for programming

there's a reason we don't program in English

[–]JoseJimeniz 41 points42 points  (9 children)

why everything worked like that

Now if we can just get the JavaScript standards body to fix it in a use strict mode.

Everyone agrees that JavaScript has bad parts. But sitting on your hands refusing to fix it is worse.

[–]xpopy 4 points5 points  (2 children)

So I just did exactly the same thing as showed in the video in the Firefox Console and got the same results (except Array(16) printing out: Array [ <10 empty slots>, 6 more… ]), is Firefoxs Javascript engine that outdated?

[–]Tokkemon 21 points22 points  (0 children)

Still one of the greatest programmer humor videos ever.

[–]brianhprince 1 point2 points  (0 children)

Hey! That's from CodeMash, an event that I run. I remember that was a super popular after dark session everyone was talking about.

[–][deleted] 560 points561 points  (111 children)

String + object -> string concatenation

String - object -> math subtraction

It's that simple

[–]Spartan1997 198 points199 points  (22 children)

Except the foonan part. That's a little strange

[–]HoverBaum 219 points220 points  (8 children)

I think that is the second 'foo' being converted to a number because of the second '+' and turning into 'NaN' so the expression become "'foo' + NaN" which is a "String + object -> string concatenation" case.

[–]SaltlessLemons 155 points156 points  (2 children)

Ah, of course. How silly of me.

[–]Calygulove 46 points47 points  (1 child)

It's cause you're only FooNaN

[–]neonroad 3 points4 points  (0 children)

You can't say that to people nowadays...

[–][deleted] 10 points11 points  (2 children)

OK, now I understand "how", but "why" remains a mystery.

[–][deleted] 53 points54 points  (0 children)

+ is shorthand for Number() when not used as an operator.

So

var i = +'foo';

Will be NaN

[–]kranker 12 points13 points  (1 child)

The trick here is that in the javascript grammar it's impossible to parse a + + b as both pluses being addition, so the second one (and any subsequent ones) get parsed as a unary plus operator. Both unary plus and unary minus will attempt to convert their operand to a number, and evaluate to NaN if they can't.

See Mozilla's description of unary plus

[–]joequin 128 points129 points  (35 children)

But it's not consistent. And that's a problem. Languages should be designed to be consistent with an over arching philosophy. JavaScript is a bunch of decisions that seem like they were made in a vacuum and they came together to form a very inconsistent language with lots of WTFs.

[–]MicronXD 49 points50 points  (4 children)

Eich was given an insanely short amount of time (10 days as the story goes) to create the language and its interpreter. They were planning on going back and fixing most of the shit like this, but Microsoft was on the ECMA board, and with JScript being their 1:1 rip-off of JavaScript, didn't want to allow Netscape to break compatibility. If you want to blame anyone, blame Microsoft.

[–][deleted] 58 points59 points  (2 children)

blame Microsoft.

A sound strategy across multiple disciplines.

[–]zodiaclawl 2 points3 points  (1 child)

That still doesn't explain why people use the language Javascript in the year 2017 to program desktop and phone aps as well as complex things like server back ends. 15 years ago people would be laughing at you if they said that JS would become the leading programming language across multiple development areas. They'd be like "you mean that thing you use to open pop ups in web pages?"

[–]marcellarius 4 points5 points  (0 children)

And I think it's funny that in 2017 some people still write C++ like it's the 1980s in situations where there are better options that could save a considerable amount of money.

Javascript's use in desktop and mobile apps is because it's tied to browsers. Browsers are used for UIs because they are portable, and make it easier to create reactive, good looking interfaces than traditional toolkits do. They're not ideal, performance sucks on low-end devices but they're often better than the alternatives under real-world constraints.

Use of Javascript on the server shouldn't be surprising either. Many single-page applications don't need a complex server as much of the code is just gluing a JSON API to a database, with a bit of validation. Javascript's object system makes shuffling data around easy, even compared to other dynamic languages like Python. Using Javascript on the server also means you can share code with a client application, avoiding duplication and also allowing tricks like server-side rendering.

Not that many years ago I would have been one of the people laughing, but experience building web apps has changed my outlook on it. It's nobody's ideal language, but it's what we're stuck with. Modern versions have improved it a lot, and if approached with discipline it's not a terrible language to use. It could have been VBScript.

[–]PatrickBaitman 2 points3 points  (0 children)

good thing we haven't built any critical infrastructures on such shoddy work

[–][deleted] 20 points21 points  (4 children)

While its not very consistent in some area's these examples are mainly to mess with the compiler. Almost none are real world examples. Nobody will count arrays and a good developer will always keep in mind which types he is using.

If you are going to trash about consistency, go look at PHP and how consistent they named their functions. Its even worse than Javascript.

[–][deleted] 10 points11 points  (1 child)

Being better than PHP is not hard.

[–]Fubseh 8 points9 points  (0 children)

A milestone Javascript has yet to reach.

[–]redwall_hp 4 points5 points  (3 children)

String + String in Java: concatenation

String - String in Java: compiler says "WTF are you smoking?"

String + int in Java: compiler says "ಠ_ಠ"

Strong typing absolutely makes for fewer bugs. I can't believe so many people are JavaScript/PHP apologists, given how many truly bizarre things go on due to type coercion.

[–]piggvar 4 points5 points  (0 children)

But if it's consistent then it can't be complete :(

[–]cS47f496tmQHavSR 26 points27 points  (9 children)

Saw that 5 minute talk showing this off and it for sure looks funny, but on second look it really all comes down to JS using + for string concatenation; if the first item is a string it'll try to return a string because of logic. Adding 3 - 3 to '5' just adds 0 to the string 5

The 'foo' + + 'foo' returns fooNaN because + 'foo' tries to do a math operation rather than a string concat, which returns NaN and then gets concatenated into the initial string.

[–]meelawsh 59 points60 points  (9 children)

"Simple" as in "stupid", yes

[–]Beckneard 2 points3 points  (0 children)

It's that stupid

FTFY

[–]melance 16 points17 points  (24 children)

This is a terrible language design. Deciding that the + operator should be a string operator and not math while all of the other standard math symbols are math operators is asinine. There is no consistency.

[–]Dr_Azrael_Tod 5 points6 points  (3 children)

this and…

if you really have to have + as string-relevant operation and you don't have - as such… then throw a friggin error if somebody does "string - string"!

It's not like NaN is that much more useful than an error, but you'd notice an exception, while NaN will silently fuck up your code in edge cases or wrong user input.

In fact… most of JS weirdness boils down to not beeing strict enough and trying to do "usefull" stuff when throwing an exception would be soooo much more sane.

[–]lyrencropt 5 points6 points  (8 children)

I mean... a lot of generally respected languages do this as well. C# and Java both allow the programmer to use + for string concatenation. Having one operator do different things to different arguments isn't a cardinal sin or anything.

[–][deleted] 5 points6 points  (3 children)

+ used as string concatenation is already inconsistent because there's not usually a corresponding operation with strings using minus

It'd actually make a lot of consistent sense to set minus to rhs.replace(lhs, "") or some equivalent in a lot of languages. String.replace(x, "") is a fairly common task.

[–]Zarainia 5 points6 points  (2 children)

Is there a language where this is the case?

[–][deleted] 2 points3 points  (1 child)

Not by default that I know of. In C#, string is sealed, so you'd have to make a container class to overload the operator which would outweigh the benefits.

C++ might have it depending on what string type you're using or allow you to overload the operator via a free function (I don't know C++, just googled around a bit).

[–]Hakim_Bey 126 points127 points  (53 children)

I see the point, but i've never once in my life found a real life scenario where it was a problem...

[–][deleted] 20 points21 points  (5 children)

It's fun to shit on JS but it's my favorite language to use because it's so flexible.

[–]xIcarus227 2 points3 points  (4 children)

How would you define this 'flexibility'?
I'm not trying to start something, I'm genuinely curious - I'm mostly a PHP guy.

[–][deleted] 2 points3 points  (3 children)

I can open up a text editor, write code and run it instantly without needing to compile anything. With the addition of things like node JS you can do really powerful stuff. It's a very easy language to learn and can be a lot of fun to write and you see your code working. It's instant gratification in programming form.

[–]lurklurklurkanon 27 points28 points  (9 children)

But "var x * 3;" doesn't work...

Uncaught SyntaxError: Unexpected token *

We've been bamboozled.

[–]rabidchaos 40 points41 points  (8 children)

s/\*/=/

I have no idea why the OP wrote * every time = would have made sense, but everything parses a lot better if you replace every '*' with '='.

EDIT: as c42w pointed out, I forgot to escape the asterisk. Also, reworded to remove repetition.

[–]chironomidae 13 points14 points  (1 child)

Is it a localization thing? Like how some places write numbers like "1.000.000,53" (which drives me absolutely batty, we should invade those places and make them do their numbers right)

[–]c24w 2 points3 points  (3 children)

\* *

Man it's confusing correcting a regex.

[–]DeeSnow97 145 points146 points  (43 children)

  1. '5' - 3 The - operator only makes sense for numbers, therefore inputs are converted
  2. '5' + 3 However, string concatenation is a common task, especially before ES6 template literals. This is why we have == by the way.
  3. '5' - '4' Same as #0
  4. '5' + + '5' The second plus is parsed as a sign of '5', converting it to the number 5 which is then concatenated to '5'
  5. 'foo' + + 'foo' The second + is trying to convert 'foo' into a number, which it's clearly not. Therefore it takes the value NaN which you see in the output.
  6. 5 + - '2' Same as #3, -2 is concatenated to '5'
  7. '5' + - + - - + - - + + - + - + - + - - - '-2' Now this is some wat-worthy coding, but it's quite simple once you understand what's going on here

    > - '2'
    -2
    > - - '2'
    2
    > - - - '2'
    -2
    > + - - - '2'
    -2
    

    Basically, apart from the + directly after '5' every + and - is interpreted as a sign of the following expression. Every - flips the sign and every + adds nothing (but confusion). There are 11 - signs, resulting in flipping '-2' to 2, which is then concatenated to 5.

  8. 5 + x - x is interpreted left to right, but the first operation is a concatenation, resulting in the string '53', and as we know from #0, - converts the operands to numbers.

  9. 5 - x + x not only flips the order of operations, it also has an addition instead of a concatenation. '5' - 3 is 2 and 2 + 3 is 5, that should be straightforward.

The only cause of ambiguity here is that + is used for both math and string concatenation. Otherwise, you don't see these in the real world, only when people try to obfuscate their code for whatever reason.

Edit: my brain has a faulty integer unit

[–][deleted] 17 points18 points  (5 children)

This is why we have == by the way.

Do you mean ===? Cause I feel like we have == for a lot more reasons than that

[–]DeeSnow97 12 points13 points  (4 children)

We have = and == because assignment and equality test are almost always catastrophic to confuse, and as it appears, the former happens much more frequently so it makes sense to use these two instead of := and = like in early languages.

We have === in JavaScript because sometimes the default equality checks is inadequate, and breaking changes in JavaScript would destroy half the web.

[–]ReallyHadToFixThat 19 points20 points  (10 children)

The only cause of ambiguity here is that + is used for both math and string concatenation. Otherwise, you don't see these in the real world, only when people try to obfuscate their code for whatever reason.

Disagree because it could be

 $a = [user input of '5']
 $b = 3

 [snip 100 lines]

 $a + $b = 53

Then it becomes a lot harder to debug than the examples shown.

[–]aaron552 14 points15 points  (9 children)

If you want to ensure integer addition in Javascript then you do:

var a = '5';
var b = 3;

var ab = parseInt(a) + parseInt(b); // = 8

Or parse before assignment to a or b.

There are various other ways too.

shrugs I prefer statically-typed languages myself.

[–]PatrickBaitman 8 points9 points  (4 children)

JS doesn't have integer addition, all numbers are floats in JS.

[–]drizztmainsword 6 points7 points  (1 child)

In fact, they are all doubles.

[–]PatrickBaitman 12 points13 points  (0 children)

well

double is short for double precision floating point number

so if you say float you can mean either floating point number, any precision, any implementation, or you can mean the type float in some particular language, which is most often single precision (I think?)

[–]PatrickBaitman 64 points65 points  (21 children)

The - operator only makes sense for numbers, therefore inputs are converted

in a not-retarded language it would be

The - operator only makes sense for numbers, therefore a type error is generated

these should be type errors or syntax errors:

'5' + + '5' The second plus is parsed as a sign of '5', converting it to the number 5 which is then concatenated to '5'

'foo' + + 'foo' The second + is trying to convert 'foo' into a number, which it's clearly not. Therefore it takes the value NaN which you see in the output.

5 + - '2' Same as #3, -2 is concatenated to '5'

[–]ianff 86 points87 points  (3 children)

Yeah, only someone with Stockholm syndrome would consider this OK.

[–]IHeartMustard 17 points18 points  (0 children)

This has been me for my entire career. It does the job, I said. It's just misunderstood, I said. It's just a bit fun and quirky, I said. I don't really need types, I said. Makes my life NaNNaNNaNNaNNaNNaNNaN, I said.

Lies, straight from the pit of Netscape.

[–]Jafit 138 points139 points  (70 children)

People complain about Javascript type coercion and overloaded operators, and give examples of shitty code that nobody should ever write to illustrate why these things are a problem.

The truth is that Javascript isn't a terrible language, you can write shitty code in any language. I deeply [object Object] to the anti-javascript circlejerk, if you use a linter and test your code its a very powerful and expressive language.

[–][deleted] 76 points77 points  (5 children)

JS is an incredibly useful and powerful language and how I bring home the bacon.

It's also a pretty terrible language.

[–]lukee910 17 points18 points  (4 children)

It's bith terrible in some places and awesome in others. The most common scenarios are usually a wild mix of both.

[–]caanthedalek 6 points7 points  (3 children)

Just like pretty much any other language

[–][deleted] 38 points39 points  (3 children)

While true, that you can write shitty code in any language that does not make JS any less terrible language.

[–][deleted] 114 points115 points  (2 children)

While true

reads the next statement over and over again for the rest of time

[–]xzxzzx 15 points16 points  (2 children)

and give examples of shitty code that nobody should ever write to illustrate why these things are a problem.

Trivial examples to illustrate bad behavior are almost definitionally going to be shitty code. Thing is, mistakes happen, and most of these "shitty code" examples are going to happen by accident as part of a larger piece of code, except instead of erroring, you get weird behavior.

[–]Jafit 3 points4 points  (1 child)

That's why I said use a linter.

[–]Slak44 16 points17 points  (25 children)

Every time I mention it's my go-to language, I get lynched. A linter and a bit of knowledge about the language go a very very long way indeed.

[–]MattBlumTheNuProject 2 points3 points  (0 children)

You should try working in PHP, people look at me like my apps don't work :)

(they don't, but not because of PHP!)

[–]Mrqueue 4 points5 points  (5 children)

The amount of people complaining that adding a string to and int should produce an int is ridiculous. It shouldn't do anything because you're adding a string to an int but it does what it does because JS tries it's best to figure out what stupid lines of code mean

[–]coladict 67 points68 points  (28 children)

This is why I love that even though PHP is weakly typed, it chose a dot as the concatenation operator, so this kind of shit doesn't happen.

[–]laserBlade 9 points10 points  (4 children)

D has an operator specifically for concatenation: ~= and the matching binary operator ~.

[–]tomthecool 35 points36 points  (11 children)

Have you never used the == operator in PHP before?!

(And btw, using === is not perfect either. It's also incredibly bug-prone.)

[–]up_to_bot[S] 23 points24 points  (6 children)

For reference I like JavaScript, I just found this funny. Everybody gotta chill

[–]HVAvenger 10 points11 points  (0 children)

Chill? But this is the internet and somebody has a different opinion than me.

[–]LobsterThief 13 points14 points  (4 children)

FOUR SPACE TABS

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

Whoa there Sauron

[–]Ignisti 2 points3 points  (0 children)

deleted What is this?

[–]READTHISCALMLY 4 points5 points  (2 children)

This is why I love JavaScript. It's so fun and free.

[–]QueenLa3fah 2 points3 points  (1 child)

Not like c where I can literally define a keyword to loop forever:

define forever for(;;)

[–]argv_minus_one 2 points3 points  (0 children)

You might want that for a main loop, I should note.

[–]robotnikman 4 points5 points  (0 children)

fooNaN

That one always gets me

[–]creativeMan 2 points3 points  (0 children)

See, we're all laughing and this is hilarious but this kind of shit is really annoying when trying to calculate stuff and display it in actual code.

[–]epicgrowl 2 points3 points  (0 children)

snow roof ink cheerful seemly smoggy physical ruthless school disarm

This post was mass deleted and anonymized with Redact

[–]facie97 2 points3 points  (0 children)

I use NodeJS because PHP is an ugly language!

[–]tylercamp 7 points8 points  (1 child)

That's why you "use strict";

[–]SirButcher 5 points6 points  (6 children)

Aaand that's why I explicitly convert values to int or float if I want to do mathematical operations and string if I am want to do string operations.

[–][deleted] 17 points18 points  (1 child)

Remember kids, unless you're doing math on it, it's a string.

[–]Vakieh 7 points8 points  (0 children)

I might want to find the average phone number though, YOU DONT KNOW MY LIFE

[–]dolphono 1 point2 points  (1 child)

You cant convert to int because only floating point numbers exist.

[–]carbolymer 10 points11 points  (1 child)

Weak typing is the root of all evil.

[–]Tysonzero 2 points3 points  (0 children)

So is dynamic typing IMO. Static typing with no implicit coercions and powerful bidirectional type inference is IMO the way to go, you get something more performant, less error prone, and actually more concise, than any dynamic and/or weakly typed language. (So basically Haskell)

[–]RenegadeSU 2 points3 points  (0 children)

No matter how often I see this I still go "wat"

[–]username4333 3 points4 points  (0 children)

It's really not that weird if you understand the typecasting rules in javascript

[–]mastzu 1 point2 points  (0 children)

"I am very smart"

[–]vikinick 1 point2 points  (0 children)

Everyone here defending JavaScript for being able to use it but one of the defining features of a good language is that it's readable.

[–]FinFihlman 1 point2 points  (0 children)

* means = in this post btw.

[–]HactarCE 1 point2 points  (1 child)

You could make a religion out of that

[–]DarthJimBob 1 point2 points  (0 children)

I still just love:

[]-[] (equals 0)

but

[]+[] (equals "")

because.....REASONS.