DEV Community

Cover image for Yes, true + true === 2. And No, JavaScript Isn’t Broken

Yes, true + true === 2. And No, JavaScript Isn’t Broken

Sylwia Laskowska on January 04, 2026

JavaScript has been my main work language for years now (and of course these days it’s mostly TypeScript 😉). I started working with it before ES6, ...
Collapse
 
bernardigiri profile image
Bernard Igiri

"computer science behavior"
No, this is not computer science behavior, these are examples of compromises common in many programming languages, often due to the legacy of the C language. Regardless, some languages do not allow this, such as: Java, C#, Rust, Go, Swift, TypeScript*, Kotlin, Scala, Dart, Ruby, Haskell, Elixir, Zig, Ada, and Fortran.

*TypeScript will complain if the boolean type is specified.

Collapse
 
sylwia-lask profile image
Sylwia Laskowska

Thanks a lot for this thoughtful comment! 🙌
You’re absolutely right - not every language allows boolean → number conversion, and many modern languages deliberately avoid it for safety and clarity. And yes, a lot of the permissive behavior in older ecosystems definitely comes from C heritage.

What I mainly wanted to highlight in the post is that this isn’t just JavaScript randomly being weird. There are quite a few languages where booleans behave numerically in some contexts, so the idea itself isn’t completely alien to programming it just feels surprising if you’re used to stricter type systems 🙂

But I totally agree with you: this behavior is a compromise, not “pure computer science truth”. Great addition to the discussion - thanks for bringing it up!

Collapse
 
kamil7x profile image
Kamil Trusiak

Coercion... :D

Since you mentioned Kyle Simpson, here is my favorite post about how this could be done better
davidwalsh.name/fixing-coercion

Collapse
 
sylwia-lask profile image
Sylwia Laskowska

Ahh, sweet memories! 😄 A 2015 article teasing the upcoming Types & Grammar — love it!
Saving this to reread and refresh my brain for sure 😀
And yeah… whenever I see the “only ===!!!!!1” mantra, Kyle Simpson instantly comes to mind - but I’ll behave and stay quiet… for now xD

Collapse
 
benjamin_nguyen_8ca6ff360 profile image
Benjamin Nguyen

Great article, Sylwia! The article makes it easier for beginners for JavaScript.

Collapse
 
sylwia-lask profile image
Sylwia Laskowska

Thanks, Ben! That's the point, always happy to help 😊

Collapse
 
benjamin_nguyen_8ca6ff360 profile image
Benjamin Nguyen

You are welcome :)

Collapse
 
mikulas_bartacek_cd3cacc2 profile image
Mikulas Bartacek

I generally agree that it's not magic, it's coercion. I just think that if someone finds this behaviour weird it's not because the booleans shouldn't be interpreted as numbers but because it's purelly implicit and JS will not tell you what it's going to do with your expressions (You need to know rules for that). In C this makes some sense, because there are no booleans. But JS has specific bool type but it is still trying to cast it into number. I think that coercion doesn't have a real use (if you need to cast it, you can do it explicitly). So yes, the true + true == 2 is logical, because what is not logical, is having implicit casting in the first place. (Maybe it has reason why it exists, but i just don't know about it). But yes, it is a thing and it will probably not be removed anytime soon.

Collapse
 
sylwia-lask profile image
Sylwia Laskowska

Totally fair points 👍 For many people the “weird” part isn’t that booleans can map to numbers, but that it all happens implicitly and silently - and yeah, that’s very much part of JavaScript’s early “be convenient, not strict” design heritage. These days I agree that explicit casting often feels clearer, especially in a world with TypeScript and stricter tooling.

And you’re also right: this behavior is basically here to stay. One of TC39’s core principles is “don’t break the web”, so changing something as fundamental as coercion would probably break a non-trivial chunk of the internet 😅

So it’s definitely a compromise - maybe not everyone loves it - but once you understand where it comes from, it stops feeling like randomness and more like a historical design choice. Thanks for the thoughtful discussion!

Collapse
 
gamelord2011 profile image
Reid Burton

This is something I saw and immedately thought of typecasting being the culprit (I do a lot of Java coding rn)

Collapse
 
sylwia-lask profile image
Sylwia Laskowska

Exactly 😄 Once you realize it’s just type coercion doing its job, it suddenly feels much less mysterious. Java training definitely helps here!

Collapse
 
simpled1 profile image
simpled1

(true + true === 2) === (1 + 1 === 2)

Collapse
 
sylwia-lask profile image
Sylwia Laskowska

Boolean gang, but make it math 😎
JS: “Trust me bro, I know what I’m doing.” ✔️

Collapse
 
alptekin profile image
alptekin I.

Thanks for this post. JS has indeed some peculiarities :). it is important to learn of these.

Collapse
 
sylwia-lask profile image
Sylwia Laskowska

Thanks a lot! 😊 And yep - JS definitely has its peculiarities, but that’s what makes it fun to learn. Always happy to see you here! 💚

Collapse
 
elsie-rainee profile image
Elsie Rainee

JavaScript isn’t broken, it’s just type coercion at work. Once you understand it, the logic makes sense.

Collapse
 
sylwia-lask profile image
Sylwia Laskowska

Exactly! 😄 It’s just coercion doing its job - once you understand it, everything suddenly feels logical.

Collapse
 
kooiinc profile image
KooiInc

Yep, type coercion. If you want to circumvent this behavior, this may be useful

Collapse
 
sylwia-lask profile image
Sylwia Laskowska

Yep, totally - type coercion doing its thing 😄 Thanks for sharing, always nice to have extra tools around!

Collapse
 
shemith_mohanan_6361bb8a2 profile image
shemith mohanan

Once you understand type coercion, true + true === 2 feels obvious, not weird.
JS isn’t broken — people just skip learning how it thinks. Looking forward to the series 😄

Collapse
 
sylwia-lask profile image
Sylwia Laskowska

Couldn’t agree more! 😄 Once you get how JS “thinks”, so many things stop being weird and start being fun. Thanks — and I’m glad you’re looking forward to the next parts!