DEV Community

Cover image for Yes, {} + [] = 0. No, JavaScript Isn’t Broken

Yes, {} + [] = 0. No, JavaScript Isn’t Broken

Sylwia Laskowska on October 29, 2026

JavaScript is a legendary programming language. Created in the mid-1990s in just 10 days, it was born during the browser wars, when Netscape was tr...
Collapse
 
sylwia-lask profile image
Sylwia Laskowska •

Ok, see you folks! 😆
airplane

Collapse
 
georgekobaidze profile image
Giorgi Kobaidze •

My aviation nerd eyes instantly spotted the Lufthansa logo 😄

Nice article by the way!🔥

Collapse
 
sylwia-lask profile image
Sylwia Laskowska •

Thank you so much! Hahaha, Giorgi, you're right, the logo is actually there! You've got a good eye! Although I have to say, I feel like Lufthansa has gone a bit downhill lately, and these days I actually prefer Polish LOT! 😄

Thread Thread
 
georgekobaidze profile image
Giorgi Kobaidze •

Yeah, I've noticed that too. It's closer to a budget airline than a luxury one.
I haven't flown with LOT Polish Airlines though.

Thread Thread
 
sylwia-lask profile image
Sylwia Laskowska •

Hahaha, on LOT you even get a sweet bun! And seriously, it's actually pretty good! I think they bake them fresh regularly. You can also get beer or wine in a plastic cup. 😅 Although on some flights they seem to keep that a secret, and you have to know to ask for it! 😂 I usually go for the wine, but there's always that fear that one unexpected bout of turbulence will leave me completely covered in it. XDDD

Thread Thread
 
georgekobaidze profile image
Giorgi Kobaidze •

You folks are on fire! A great country with an airline THAT good... Poland is really out there going for being the best country in Europe. And it's working. 😄

Collapse
 
francistrdev profile image
FrancisTRᴅᴇᴠ •

This is the most JavaScript thing I've seen, and I am not disappointed XD

Collapse
 
sylwia-lask profile image
Sylwia Laskowska •

Hahaha, Francis, this is just the tip of the iceberg when it comes to JavaScript weirdness!

Collapse
 
xulingfeng profile image
xulingfeng •

That’s weird. I never get notifications for your posts. I only found this fantastic article more than ten hours late. Must be a bug on Dev.to!😂😍

Collapse
 
sylwia-lask profile image
Sylwia Laskowska •

Hahaha, who knows, maybe it's all part of some conspiracy! 😂

Collapse
 
kenwalger profile image
Ken W Alger •

JavaScript is one of those languages where you can explain exactly why something happens, walk through every step of the specification, demonstrate that the implementation is behaving correctly, and still conclude that the language has made some questionable life choices.

The distinction between {} + [] and ({} + []) is particularly entertaining. Same characters, different parsing context, entirely different result.

Glad to see the series back! If this is what two hours at Frankfurt Airport produces, perhaps longer layovers should be encouraged.

Collapse
 
sylwia-lask profile image
Sylwia Laskowska •

Hahaha, exactly! Everything makes sense, everything has a perfectly logical explanation, and yet it never stops being funny, even after all these years!

And as for airports, I actually quite enjoy writing there! It's only when I'm at an airport that I realize just how much time writing articles really takes. Somehow, when I'm at home, I don't notice it nearly as much. 😂

Collapse
 
pengeszikra profile image
Peter Vivo •

Hi Sylwia! JS is my favorite programming language, as you know, since Adobe killed Flash. But honestly, JS is better because it’s free to use. Maybe you could try TDM = Travel Development Method, which is also a vibe coding aberration. It means nothing more than programming while travelling. By the way, I’m happy to have found a similar concept in UMGD.

But let’s get back to JS. I’m a really big fan of it, which is why I argue for JSDoc instead of TS. These positive feelings also led me to create mordorjs, which, at the end of the day, is pure JS. It just uses a strange ability of JS that lets you write working code in a one-character-wide format.

Collapse
 
sylwia-lask profile image
Sylwia Laskowska •

Hahaha, Peter, yes, I know how much you love JavaScript! I personally use TypeScript, but I totally get your point!

Actually, just the day before yesterday, I was chatting with some fellow developers in Munich, and we started reminiscing about the old days when TypeScript was just emerging. I remember we were a bit hesitant to adopt it because, well, what if Microsoft decided to abandon the project?

But what eventually convinced us was that TypeScript compiled down to pretty clean, readable JavaScript. That was our main argument: we could always go back to that slightly crazy but beautiful language!

Collapse
 
pengeszikra profile image
Peter Vivo •

Right now thanks to open script behiaviour of TypeScript any one can fork it, and make it own verison with own solution, so right now TS is bit versatile compared to JS, there is need to wait a new upgradding in every years.

Collapse
 
shubhradev profile image
Shubhra Pokhariya •

Imagine explaining this in a code review: "No, the empty block isn't useless. It's what gets us zero 😂" Also love that adding just one pair of parentheses makes JavaScript treat {} as an object. Can't wait for the {} + {} one, Sylwia!

Collapse
 
sylwia-lask profile image
Sylwia Laskowska •

Hahaha, I doubt it would even make it to code review! More likely, someone would be sitting there wondering, "Why the hell isn't this code working?!" 😂

Collapse
 
nyaomaru profile image
nyaomaru •

JavaScript has so many deep parts or maybe shallow parts? 😹

I always find it funny how something as simple as an empty object and an empty array can behave so differently depending on how the parser sees them.

These little quirks are one of the reasons JavaScript keeps surprising us. 🐟

As always, your article was really easy and fun to read!

Have a safe trip home! ✈️😸

Collapse
 
sylwia-lask profile image
Sylwia Laskowska •

Aww, thank you! ❤️ I'm already back home! :) And exactly! This kind of knowledge probably won't be very useful in our everyday coding. Although I remember there was (and I think there still is) a website called Return True to Win, where knowing these weird JavaScript tricks actually came in handy!

I remember joking with my colleagues at work that one day we'd give someone those challenges during a job interview. And then, a few years later, someone actually gave me one of those challenges in an interview! 😂

Collapse
 
nyaomaru profile image
nyaomaru •

Haha, that’s amazing 😹
The fact that a joke challenge came back as a real interview question years later is peak JavaScript. 😹

And glad you made it home safely! 😸

Collapse
 
sinarezaei profile image
Sina Rezaei •

One thing worth remembering is that JavaScript's object semantics can become a production security issue, not just a source of funny console output.

There's a good example in qs (CVE-2026-82417). An attacker could influence an object's shape through query parameters, and later qs.stringify() would try to invoke constructor.isBuffer without checking whether it was actually callable. That could trigger a TypeError and cause a DoS.

It's a different edge of JavaScript than {} + [], but the underlying lesson is similar: what looks like a perfectly ordinary object isn't necessarily what the next operation expects.

That's why I think understanding these language rules matters beyond passing a JavaScript interview.

Collapse
 
sylwia-lask profile image
Sylwia Laskowska •

Oh, I didn't know about this one! But that's exactly the thing with JavaScript. It's not always just about funny console tricks. Sometimes making assumptions about objects can have some really serious consequences!

Collapse
 
sinarezaei profile image
Sina Rezaei •

There's another interesting edge case with coercion: Symbol.toPrimitive. It's not just about how an object gets converted to a string or number. An object can actually control what happens during that conversion.

I've seen this become particularly relevant with custom value objects in financial systems. Imagine a Money object used in a calculation, but its primitive conversion returns a formatted string for display. Suddenly, an expression like total + amount can switch from numeric addition to string concatenation, depending on which object is involved.

That's one reason I prefer explicit methods like money.add(other) and money.toMinorUnits() over relying on implicit conversion. The language is doing exactly what the spec says, but the business logic might be doing something completely different from what the developer intended.

Collapse
 
dhiraj-shrotri profile image
Dhiraj Shrotri •

I dislike people who make fun of JS as a coding language by point out its weird parts. JavaScript was originally designed to be a lightweight scripting language that allowed non-programmers to add interactivity to web pages. Over time, it evolved far beyond that original purpose and now powers everything from frontend applications and backend services to mobile apps and desktop software.

I think it's unfair to judge a language solely by its weirdest edge cases, especially when you consider how much its scope has expanded over the years. If you take almost any language and stretch it to support such a wide variety of use cases, you're bound to accumulate some quirks along the way. Just look at Python!

On a related note, I absolutely love this podcast episode about the origins of JavaScript. It's a fascinating listen and provides some great historical context for why the language is the way it is.

The Origins of JavaScript

Collapse
 
sylwia-lask profile image
Sylwia Laskowska •

But that's exactly what I wrote about in the article! How JavaScript came to be, how much it has evolved over the years, and why it still has to preserve all those quirks for the sake of backward compatibility.

And besides, think about how much I must love this language if I'm willing to dig into its weirdest corners and explain exactly why they work the way they do!

Making fun of JavaScript's quirks doesn't mean I don't appreciate it. Quite the opposite! ❤️

Collapse
 
benrrr profile image
Brrrrr •

JS being JS

Collapse
 
sylwia-lask profile image
Sylwia Laskowska •

How not to love it 😅

Collapse
 
jamescarter11 profile image
James Carter •

Great explanation! JavaScript's type coercion can be confusing, especially for beginners, but examples like these make the underlying rules much easier to understand. I also appreciate the distinction between {} as an empty block and an object literal. Definitely a fun reminder to pay attention to context when writing JavaScript!

Collapse
 
sylwia-lask profile image
Sylwia Laskowska •

Oh, absolutely! 😄 These days, most of us use TypeScript, and it sometimes feels like we're pretty well protected against JavaScript's weirdness in everyday development. But you never know, over the years, you end up working on all kinds of projects!

Collapse
 
robertpaschal profile image
Odinaka Robert Nnamani • • Edited

The sneaky part is +[] detouring through "" on its way to 0, same reason an empty form field sails past a Number() check

Collapse
 
sylwia-lask profile image
Sylwia Laskowska •

Exactly! 😂 JavaScript has been turning empty things into zero for 30 years, and we're still finding new ways to get surprised by it.

Collapse
 
robertpaschal profile image
Odinaka Robert Nnamani •

Thanks, Sylwia. That empty to zero path keeps showing up in the places you least expect it.

Collapse
 
effessdev profile image
EffessDev • • Edited

This is why I always write everything explicitly 😅 It's not a problem as long as we avoid it right?

Collapse
 
sylwia-lask profile image
Sylwia Laskowska •

Exactly! That's one thing! 😅 I also use TypeScript and linters to keep me (or my coding agent) in check whenever we get a little careless. Do you use TypeScript too?

Collapse
 
effessdev profile image
EffessDev • • Edited

I only use TypeScript (I literally forgot JavaScript lol), and I'd never do something like {} + [] (too risky 😬😅)

Collapse
 
leob profile image
leob •

Wow! The fact that {} isn't an object at all, and that the + is unary - I didn't see that one coming! Might be logical "when explained after the fact", but I'm finding this truly bizarre ...

Collapse
 
sylwia-lask profile image
Sylwia Laskowska •

Exactly! 😂 Just because you understand the logic behind it doesn't make it any less bizarre! I mean... well, it's JavaScript. XDDD

Collapse
 
javierolazaran profile image
Javier Olazarán •

This is the longest "it's not a bug, it's a feature" I've ever read.