-1

Currently I'm working on an Angular 4/Typescript project. Yesterday, while debugging a part of the application, I saw an operator I've never seen before:

enter image description here

Obviously I asked the other team members about it and they couldn't provide an answer, they just told me that it seemed to be some sort of special operator.

Today I've been googling about it withouth any success. I also checked on the Node.js console and it appears to be just as expected, a double negation that simply converts truthy to true and falsy to false. Wouln't these two statements produce the same effect?

if (x) // do something

if (!!x) // do something

Hence my question: It is actually some sort of special operator or workaround to get something done, or it's just a missconcenption of some developer?

mdarefull
  • 829
  • 2
  • 14
  • 24
  • 1
    It's just a double negation. The ! operator is often used to convert something to a boolean in JavaScript. – Okoch Jan 31 '18 at 13:06
  • I think first not operator converts the value to a boolean, and the second not operator will negate it. – Taha Paksu Jan 31 '18 at 13:06
  • 2
    @Taha That sounds way more complicated than it is. `!` converts any value to a boolean `true` or `false` and `!` negates that again, essentially it's a *truthy/falsey* to `true`/`false` conversion. Which is entirely superfluous here and doesn't do anything. – deceze Jan 31 '18 at 13:08
  • Yep I couldn't be so sure so I removed the last sentence. Got an headache just now :) – Taha Paksu Jan 31 '18 at 13:09
  • 2
    Sidenote: if *one* junior developer can't figure it out… fair enough. If an entire *team* can't… :-/ – deceze Jan 31 '18 at 13:09
  • @deceze, BTW I think the first operator doesn't convert it to a boolean, it negates the variable converted to a boolean. Is it right? – Taha Paksu Jan 31 '18 at 13:11
  • @Taha Well, yes, it converts it to the *opposite* boolean. – deceze Jan 31 '18 at 13:12
  • @deceze I did figure it out, but as you said, I simply assummed it has to be some sort of special technique, because, as you have said, in this case it doesn't make any sense. – mdarefull Jan 31 '18 at 13:13
  • 1
    @mdarefull Completely off-the-rails warning: if the answer you got was "probably some special operator" and nobody deleted it, your team may be prone to [cargo-cult programming](https://en.wikipedia.org/wiki/Cargo_cult_programming). Beware. – deceze Jan 31 '18 at 13:15
  • @deceze Thanks a lot! At least I got something useful out of a dup question :-) – mdarefull Jan 31 '18 at 13:18

2 Answers2

4

It's a double boolean negation - basically a cheap way to turn any value into a pure boolean true/false.

Michael Chaney
  • 2,911
  • 19
  • 26
0

It's just a double negation. The ! operator is often used to convert something to a boolean in JavaScript.

var undefinedVar = undefined;
console.log(!undefinedVar);
console.log(!!undefinedVar);

var nullVar = null;
console.log(!nullVar);
console.log(!!nullVar);

var trueVar = true;
console.log(!trueVar);
console.log(!!trueVar);

var falseVar = false;
console.log(!falseVar);
console.log(!!falseVar);
Okoch
  • 445
  • 2
  • 5