DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’»

DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’» is a community of 970,177 amazing developers

We're a place where coders share, stay up-to-date and grow their careers.

Create account Log in
Hidayt Rahman
Hidayt Rahman

Posted on

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)

This post blew up on DEV in 2020:

js visualized

πŸš€βš™οΈ JavaScript Visualized: the JavaScript Engine

As JavaScript devs, we usually don't have to deal with compilers ourselves. However, it's definitely good to know the basics of the JavaScript engine and see how it handles our human-friendly JS code, and turns it into something machines understand! πŸ₯³

Happy coding!