I came across some JavaScript code that uses two exclamation marks together like this:
this.horizontal = horizontal !== undefined ? !!horizontal : this.horizontal;
I’m not familiar with the !! syntax and wondering what it actually does. Is this some special JavaScript operator or just two separate operators used together? Can someone explain how this works and when I might want to use it in my own code? I’ve seen it in a few different places now but haven’t been able to figure out its exact function.
the !! operator is a neat trick for converting values to bools. first ! flips the value, then the second ! flips it back. so !!“text” is true while !!0 is false. def useful in many cases!
I ran into this recently too and was confused at first! The double bang forces JavaScript to return actual true/false instead of all the weird truthy/falsy stuff JS normally does. Much cleaner than writing Boolean(value).
Been coding JavaScript for years and thought the double bang was just fancy syntax, but it actually fixes a real problem with JS’s type system. The difference between !! and regular truthiness checks? You get an actual boolean primitive instead of relying on implicit conversion.
This matters when you’re doing strict equality checks or storing values in objects where consistent data types matter. I’ve hit bugs where functions expected booleans but got strings or numbers, breaking things later.
That pattern’s super common in constructor functions or with optional parameters. Instead of undefined floating around your object properties, you get clean true/false values that work predictably everywhere.
The double bang normalizes boolean values across different JavaScript environments and libraries. I’ve seen frameworks and older browsers handle truthy/falsy conversion differently, but !! gives consistent results everywhere. It’s also faster than Boolean() or ternary operators for simple boolean conversion. When processing large datasets or running frequent validations, those microseconds add up. In your example, !! ensures this.horizontal is definitely true or false, not some other truthy value that might break things later. This matters when serializing to JSON or passing data between different parts of your app where type consistency is critical.
The double bang is a lifesaver for data validation and API responses where you need consistent boolean handling.
I deal with this all the time processing forms and user inputs. You get messy values - empty strings, null, undefined, or string booleans like “false”. The !! operator turns everything into clean true/false values.
Take form submissions - checkbox values might be strings or completely missing. Using !! makes your boolean logic work predictably.
The real win is automating this across multiple inputs or API endpoints. Instead of custom validation for each field, you build workflows that automatically apply this conversion to sanitize incoming data.
I’ve built automated pipelines for exactly this. With webhook data or form submissions, consistent boolean conversion saves hours of debugging later.
Latenode makes these data transformation workflows really smooth to implement and maintain: https://latenode.com
You’re looking at two NOT operators chained together - it’s a neat type coercion trick. The first ! flips any value to its boolean opposite, then the second ! flips it back to get the actual boolean. Super handy when you’re working with APIs or functions that want explicit booleans instead of just truthy/falsy values. I use this all the time for conditional assignments when I need clean boolean state. It handles all of JavaScript’s weird falsy values consistently - empty strings, null, undefined, zero all become false, everything else becomes true. Way cleaner than writing verbose if-else blocks for conversion.