Code Smell 69 - Big Bang (JavaScript Ridiculous Castings)

This handy operator is a trouble maker.

Subscribe to my newsletter and never miss my upcoming articles

Listen to this article

TL;DR: Don't mix booleans with non-booleans.


  • Not Declarative Code

  • Hard to debug

  • Magic Castings

  • Accidental Complexity


  1. Be Explicit

  2. Don't mix Booleans with non-booleans.

  3. Fail Fast

  4. Be Smarter than your compiler.

  5. Stay loyal to the bijection.

Sample Code



Since this is a "feature" in some languages it would be hard to test. We can set programming policies or choose more strict languages.

We should detect ! !! usages in non-boolean objects and warn our programmers.


  • Casting

  • Coercion

  • Javascript


Languages like JavaScript divide their whole universe into true or false values. This decision hides errors when dealing with non booleans.

We should be very strict and keep booleans (and their behavior), far away from non booleans.


More info


Photo by Greg Rakozy on Unsplash

It is easier to write an incorrect program than understand a correct one.

Alan J Perlis

This article is part of the CodeSmell Series.

Comments (1)

Mr Rampage's photo

You might be interested in this. Using the boxed version Boolean can yield unpredictable results. What's your take on using boxed vs. primitives? Are there any use cases for boxed over primitives in JavaScript?

See this bug found in Jest. It's a pretty crazy bug.

const s1 = new Set([false, true]);
const s2 = new Set([new Boolean(true), new Boolean(true)]);

expect(s1).not.toEqual(s2); // success

expect(s2).not.toEqual(s1); // failure
//    expect(received).not.toEqual(expected)
//    Expected: Set {false, true}
//    Received: Set {{}, {}}const s1 = new Set([false, true]);