1

I would like to understand what is the real use of !! in JavaScript. I tried with few different validations to understand the purpose of !!. But all tries resulted in same output.

var a; // undefined
if (a) {
    console.log('1');
} else {
    console.log('2');
}

if (!!a) {
    console.log('1');
} else {
    console.log('2');
}

For all the following scenario, I get only 2 printed in console. a = null; a = '';

Could anyone please explain this in detail?

Rajan
  • 111
  • 6
  • 4
    It convert the type to `Boolean` type – Ankit Agarwal Nov 27 '18 at 11:29
  • You're just applying the ! operator twice. This will convert any variable to a boolean. – ShamPooSham Nov 27 '18 at 11:33
  • @Ankit, ShamPooSham Thanks for your answers. I understand the point. Assume this scenario, var a = 2; console.log(a); ----> returns 2 console.log(!a); -----> returns false console.log(!!a); ------> return true so what is the real point in using this !! – Rajan Nov 27 '18 at 11:42
  • @Rajan That information is in the duplicate which is linked at the top of your question. – Ivar Nov 27 '18 at 12:51

0 Answers0