patternjavascriptCritical
What does the !! (double exclamation mark) operator do in JavaScript?
Viewed 0 times
doublemarkthedoesoperatorwhatjavascriptexclamation
Problem
I saw this code:
It seems to be using
this.vertical = vertical !== undefined ? !!vertical : this.vertical;
It seems to be using
!! as an operator, which I don't recognize. What does it do?Solution
It converts
So
It is generally simpler to do:
Real World Example "Test IE version":
If you ⇒
But if you ⇒
Object to boolean. If it was falsy (e.g., 0, null, undefined, etc.), it would be false, otherwise, true.!object // Inverted Boolean
!!object // Noninverted Boolean, so true Boolean representationSo
!! is not an operator; it's just the ! operator twice.It is generally simpler to do:
Boolean(object) // BooleanReal World Example "Test IE version":
const isIE8 = !! navigator.userAgent.match(/MSIE 8.0/);
console.log(isIE8); // Returns true or falseIf you ⇒
console.log(navigator.userAgent.match(/MSIE 8.0/));
// Returns either an Array or nullBut if you ⇒
console.log(!!navigator.userAgent.match(/MSIE 8.0/));
// Returns either true or falseCode Snippets
!object // Inverted Boolean
!!object // Noninverted Boolean, so true Boolean representationBoolean(object) // Booleanconst isIE8 = !! navigator.userAgent.match(/MSIE 8.0/);
console.log(isIE8); // Returns true or falseconsole.log(navigator.userAgent.match(/MSIE 8.0/));
// Returns either an Array or nullconsole.log(!!navigator.userAgent.match(/MSIE 8.0/));
// Returns either true or falseContext
Stack Overflow Q#784929, score: 3748
Revisions (0)
No revisions yet.