DEV Community

Sylwia Laskowska
Sylwia Laskowska Community Curator

Posted on

Beginner’s Guide #1: if (data) vs if (!!data) in JavaScript 🤔

Many beginners (and even some more advanced developers) run into this very common case in software development.

We want to execute some code only if a variable has a value.

There are two ways to write this check:

if (data) {
  // do something
}
Enter fullscreen mode Exit fullscreen mode

and

if (!!data) {
  // do something
}
Enter fullscreen mode Exit fullscreen mode

Both are correct ✅ and will give the same result.
The difference is style and intention.

✨ if (data)

This is the cleanest and most idiomatic way.
JavaScript automatically converts the value to true or false.
It’s simple, short and very readable.

🔍 if (!!data)

Here we force an explicit conversion to boolean.
Maybe a bit more descriptive, since it shows that we definitely want a true/false value.

👉 But inside an if statement this is redundant – because if already does that conversion for us.

⚡ Performance?

Let’s be honest. These two will always give the same result.
And even if there was some micro difference in performance, it’s negligible – you’ll never notice it, even inside loops.

So in practice, the shorter if (data) is the more elegant one. But is it only about syntax? 🤔

🧩 Falsy values explained

When I see someone using !! in an if, I sometimes wonder if they really understand type coercion and falsy values in JavaScript.

Falsy values are those that, in a logical context (if, &&, ||, !, Boolean()), always evaluate to false.

Here’s the full list (there are only 8 of them! 🎯):

false
0
-0 (negative zero)
0n (BigInt zero)
"" (empty string)
null
undefined
NaN
Enter fullscreen mode Exit fullscreen mode

Everything else is truthy — it becomes true when converted to boolean.

⚖️ Examples

"0"         // truthy (string, not number 0)
"false"     // truthy (string, not boolean false)
[]          // truthy (empty array is still an object)
{}          // truthy (empty object too)
function() {}  // truthy
Enter fullscreen mode Exit fullscreen mode

✅ Conclusion

If you learn this list of falsy values, you’ll probably start to feel that the simpler if (data) is more elegant ✨.

And even if you still like the !! style, at least you’ll be using it consciously, not out of confusion.

💡 Bonus: When !! actually makes sense

The !! - the logical NOT operator applied twice - is not useless! It shines when you need to explicitly store or return a boolean value:

const isActive = !!data;

In this case, it’s perfectly fine and very clear ✅.

🚀 Thanks for reading! Hopefully now you won’t overuse !! in your ifs 😉

Top comments (2)

Collapse
 
jonrandy profile image
Jon Randy 🎖️ •

!! is not an operator - you are using the ! operator twice.

Collapse
 
sylwia-lask profile image
Sylwia Laskowska •

Ah, good catch! I updated the post for more clarity — thanks for pointing it out 🙌