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, ...
For further actions, you may consider blocking this person and/or reporting abuse
"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.
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!
Coercion... :D
Since you mentioned Kyle Simpson, here is my favorite post about how this could be done better
davidwalsh.name/fixing-coercion
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
Great article, Sylwia! The article makes it easier for beginners for JavaScript.
Thanks, Ben! That's the point, always happy to help 😊
You are welcome :)
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 == 2is 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.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!
This is something I saw and immedately thought of typecasting being the culprit (I do a lot of Java coding rn)
Exactly 😄 Once you realize it’s just type coercion doing its job, it suddenly feels much less mysterious. Java training definitely helps here!
(true + true === 2) === (1 + 1 === 2)
Boolean gang, but make it math 😎
JS: “Trust me bro, I know what I’m doing.” ✔️
Thanks for this post. JS has indeed some peculiarities :). it is important to learn of these.
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! 💚
JavaScript isn’t broken, it’s just type coercion at work. Once you understand it, the logic makes sense.
Exactly! 😄 It’s just coercion doing its job - once you understand it, everything suddenly feels logical.
Yep, type coercion. If you want to circumvent this behavior, this may be useful
Yep, totally - type coercion doing its thing 😄 Thanks for sharing, always nice to have extra tools around!
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 😄
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!