DEV Community

Hidayt Rahman
Hidayt Rahman

Posted on

3 3

Convert any type into boolean — JS

!!
it’s used to convert something to boolean

Converts Object to boolean. If it was falsy (e.g. 0, null, undefined, etc.), it will be false, otherwise, true.

So !! is not an operator, it's just the ! operator twice.

It may be simpler to do:

Boolean(object) // boolean

Real-World Example “Test IE version”:

const isIE8 = !! navigator.userAgent.match(/MSIE 8.0/);  
console.log(isIE8); // returns true or false
Enter fullscreen mode Exit fullscreen mode

If you ⇒

console.log(navigator.userAgent.match(/MSIE 8.0/));  
// returns either an Array or null
Enter fullscreen mode Exit fullscreen mode

But if you ⇒

console.log(!!navigator.userAgent.match(/MSIE 8.0/));  
// returns either true or false
Enter fullscreen mode Exit fullscreen mode

Top comments (0)

Postgres on Neon - Get the Free Plan

No credit card required. The database you love, on a serverless platform designed to help you build faster.

Get Postgres on Neon

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay