Categories
JavaScript Quick Tips Quick Tips

Ditch !! and try Boolean() instead

< 1 min read

Using !!someVariable might seem smart to turn a value into a boolean in JavaScript, but is it intuitive for someone who doesn’t know this JavaScript "trick" to understand?

Try using Boolean(someVariable) to make your intentions easier for everyone to understand. Write JavaScript for humans 🙌

code

I’ve created a test script which confirms that Boolean() and !! behave the same way: https://gist.github.com/simonplend/be1f26a83a1b0769bd374e14d244aa07

Original tweet