Yes, true + true === 2. And No, JavaScript Isn’t Broken
TL;DR
JavaScript's `true + true === 2` occurs because the `+` operator defaults to numeric addition when no strings are involved, converting `true` to 1 and `false` to 0. This behavior is common in many programming languages, not a bug.
Tags
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
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.
-
true→1 -
false→0
So:
true + true
// becomes
1 + 1
// which equals
2
This isn’t random. Many languages treat booleans as numeric under the hood:
- Python:
True + True === 2 - C / C++:
truebehaves as1
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
Once you know booleans convert to 1 and 0, nothing here is magical anymore.
Practical takeaway
-
+performs numeric addition unless strings are involved -
trueconverts to1,falseconverts to0 - 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 😎