DEV Community

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

Posted on

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

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, back when the language felt a lot more “primitive” than it does today – no let or const, no spread operators, no Promises, not even classes, only prototypes.

Writing JS back then was pure chaos in the best and worst sense – a total roller coaster where you really had to understand the language to avoid shooting yourself in the foot. I remember reading a ton about it. The books that really stuck with me were You Don’t Know JS (later You Don’t Know JS Yet) by Kyle Simpson. You can still find them on GitHub today, and they’re an absolute goldmine of JavaScript knowledge.

So nowadays, when yet another backend colleague laughs at JS quirks like true + true === 2, I just smile politely and nod… while internally screaming: “but it’s completely obvious, because this and that and also…” xDDD I used to actually try to explain it, but usually after about 10 seconds they’d just stare at me like I’d lost my mind, so I gave up 😅

But here, on dev.to, I’m hoping I’ll find people who actually want to hear the explanation 😂

I could write one long article about a bunch of JavaScript oddities, but how about a weekend series instead? If you like the idea, hit the like button 😉

Why true + true === 2

Here’s one of JavaScript classics people love to joke about:

true + true === 2 // true
Enter fullscreen mode Exit fullscreen mode

At first glance, this feels ridiculous.
How can adding two booleans give you a number?


The key: + is (mostly) a numeric operator

In JavaScript, the + operator works in two modes:

  • if there’s a string involved → string concatenation
  • otherwise → numeric addition

Since true isn’t a string, JavaScript assumes this is math and converts both values to numbers.


And what is the numeric value of true?

JavaScript uses a standard internal conversion rule called ToNumber.

  • true1
  • false0

So:

true + true
// becomes
1 + 1
// which equals
2
Enter fullscreen mode Exit fullscreen mode

This isn’t random. Many languages treat booleans as numeric under the hood:

  • Python: True + True === 2
  • C / C++: true behaves as 1

So again, JavaScript is not being uniquely weird here - it’s actually pretty normal computer science behavior.


It also explains other “strange” cases

For example:

true * 10   // 10
false * 10  // 0
true - 1    // 0
Enter fullscreen mode Exit fullscreen mode

Once you know booleans convert to 1 and 0, nothing here is magical anymore.


Practical takeaway

  • + performs numeric addition unless strings are involved
  • true converts to 1, false converts to 0
  • it’s not JavaScript being silly - lots of languages behave similarly

And that’s why true + true === 2 is not a bug… just logic wearing a funny hat 😎

Top comments (18)

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
 
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
 
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
 
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
 
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
 
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
 
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
 
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!