r/ProgrammerHumor • • Aug 01 '22

>>>print(“Hello, World!”)

Post image
60.8k Upvotes

5.7k comments sorted by

View all comments

1.3k

u/[deleted] Aug 01 '22

Javascript {([][])+()}

1.8k

u/a-slice-of-toast Aug 01 '22

creates squares

1.1k

u/[deleted] Aug 01 '22

With how much sense JavaScript makes sometimes, you could very well be right.

269

u/SirNerfsALot Aug 01 '22 edited Aug 01 '22

My favorite programming quip from a friend: "JavaScript is not that bad if you have a margarita or two before you look at it."

Edit: forgot the "not"

20

u/mrloooongnose Aug 01 '22

If you need a drink to understand JavaScript code, it usually means that you had too many drinks while writing JavaScript code.

9

u/tehlemmings Aug 01 '22

When I was in uni I was involved in running a bunch of community websites. I used to get drunk and write hacky javascript bullshit to interact with our websites in interesting but isolated ways.

When we sobered up in the morning we could never understand what the fuck I wrote. And if we tried to 'fix' it, make it make sense to a sober person, it'd stop working.

Javascript has been my goto "I'm drunk and want to program something dumb" language for like, 15 years now lol

3

u/Deon2137 Aug 01 '22

make it double and vodka glasses

→ More replies (10)

38

u/Jake0024 Aug 01 '22

That's 90% of JS let's be real

2

u/alextremeee Aug 01 '22

How about: console.log( ‘b’ + ‘a’ + + ‘a’ ).toLowerCase();

→ More replies (2)

238

u/Alzyros Aug 01 '22

I wouldn't be surprised if that resolved to 3 or something

40

u/FirstSineOfMadness Aug 01 '22

http://www.jsfuck.com/ would like to have a word

7

u/Cat_Marshal Aug 02 '22

What is wrong with programmers

→ More replies (1)

87

u/human-potato_hybrid Aug 01 '22

What does this actually do tho

117

u/ImNotABot-Yet Aug 01 '22

Tried a codepen and just got the mundane:

SyntaxError: Unexpected token ']'

46

u/SuperFLEB Aug 01 '22 edited Aug 01 '22

Yeah, the "[][]" isn't anything. There are some languages where that'd let you add something to the end of an array (in this case, an anonymous, empty array), akin to push, but JS ain't one of them.

[][[]], works, though. [] as a number is "0", so [][[]] reads as [][0], which asks for the first element of an empty array, and gives undefined. That said, +() is invalid as well, so the upthread still isn't doing anything if you use [][[]].


Playing with things more:

Now, what I can't figure out is why I get undefined from [[]][[]]. I'd think that'd factor down to Array( Array() )[0] and return an empty array, but it returns undefined instead. [[]][0] returns [] as expected, but not [[]][[]].

I suspect I might be wrong about how the above one is working, too, that the [[]] isn't coercing to 0 in either case, like I thought it was, and the undefined is coming from some different mechanism.

5

u/big_bad_brownie Aug 01 '22

[0,1,2,3][[n]]

Returns the nth element in the array.

I think [0,1,2,3][[]] is evaluated as [0,1,2,3][undefined]

4

u/danielv123 Aug 01 '22

Because arrays only support integer and string indexes. Array(0) is not an integer, so its converted to a string. [].join() === "", so you run [0,1,2][""], which is undefined. You can do array = []; array[""] = "Test"; console.log(array[[]]) though.

→ More replies (3)

3

u/XNocken Aug 01 '22

The empty array inside the array is casted to an empty sting because it calls toString on the array which returns a comma seperated list of all entries. Because its empty it just returns an empty string. That means that its not accessing index 0 its accessing Index empty string. It works if you test it with an object

→ More replies (2)
→ More replies (3)

7

u/textreader1 Aug 01 '22

Type coersion - this video explains it in depth

6

u/NoirDust Aug 01 '22

i dont want it in depth i just want a rough idea

→ More replies (2)
→ More replies (2)

40

u/possibly-a-pineapple Aug 01 '22

throw an error?

15

u/feckOffMate Aug 01 '22

Are you just saying it’s JavaScript or is there actually a JavaScript object that I had no idea about?

Edit: I guess I could have just tested it myself if I’d actually do some damn work.

→ More replies (2)

2

u/Megaforce4win Aug 01 '22

Syntax error

2

u/block36_ Aug 01 '22

Where you trying to do something like this?

([][[]]*(+[])+'')[+[]]

Returns ‘N’

There used to be another way but it seems like something was changed.

→ More replies (1)
→ More replies (7)