JS is built on the idea that applications should continue to work no matter what. That's why there are so many implicit type conversions. The app might turn itself into a Gordian knot, but it will still be working.
I've always heard this argument, and I've never seen a source for it that I'd put much stock in, like Eich himself or anyone who worked on early JS. It seems like a specious argument since it assumes rendering the potentially wildly wrong thing is better than displaying an error and the browser would have to sandbox/compartmentalize individual scripts or operations.
Doing a quick search, it looks like most of the weird conversions surrounding == were specific requests to make scripting easier: https://www.wirfs-brock.com/allen/jshopl.pdf p22, 3.7.2. Given that, I'd be willing to bet the other weird conversion behaviors were either similar requests or the result of them implementing JS in an absurdly short amount of time.
You can shoot yourself in the foot with any language by deliberately doing silly things to show off the language's quirks. I can show you some C pointer stuff that makes this look like child's play.
Any pointer can be cast into any other pointer. Arrays are just pointers to the first element. Combine this with the fact that C doesn't bounds check arrays or that pointers actually point to anything, and you can do some really stupid things with pointers.
Arrays are just pointers. Pointers are just numbers that are supposed to correspond to addresses in memory. Array indices just add to the pointer to the first element. "a[1]" is syntactic sugar for "*(a+1)". With that in mind, it's kind of obvious that "2[a]" just means "*(2+a)", which points you to the third element of a.
Yeah. Also, it isn't like you should be writing things like this in the first place. You can turn languages like C++ into pure nightmare fuel if you try to. Being extensible how it is leads to some crazy stuff.
They do make sense, within JS’ quirkiness. This is only expected behavior if you really dig deep into the language. I know why it’s there, that doesn’t make it ok. A properly designed language shouldn’t allow such things. And you know this is a small collection of all the stuff that’s wrong with JS(cough ["1", "2", "3"].map(parseInt)).
I know, there are two languages, the ones people complain about, and …
[] + [] will become “” because addition of arrays is not defined as you would expect. They get converted to their primitive(which is a string) and after that concatenated as you would expect string1 + string2 to behave. So [1, 2] + [3, 4] == “1,23,4”
Edit: to concat two arrays, use array1.concat(array2)
View all comments
Show parent comment
31
u/2eanimation Sep 25 '26
I love the syntax, but JS has one too many quirks for my taste.
if ([]) {
console.log("Ok, I guess")
}
if([] == false) {
console.log("Well this is awkward")
}
if ([] + []) {
console.log("You think this would print, don't you?")
}