Convert any type into boolean — JS

Hidayt Rahman
Jun 19, 2022

--

!!

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

If you ⇒

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

But if you ⇒

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

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

Hidayt Rahman
Hidayt Rahman

Written by Hidayt Rahman

A passionate UI Engineer, Love Travelling and Photography

No responses yet

Write a response