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
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.
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.
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
1.3k
u/[deleted] Aug 01 '22
Javascript {([][])+()}