javascript tutorial - [Solved-5 Solutions] What is the !!(not not) operator in javascript ? - javascript - java script - javascript array



Problem:

We saw some code that seems to use an operator We don't recognize, in the form of two exclamation points, like so: !!. Can someone please tell me what this operator does?

The context in which We saw this was,

this.vertical = vertical !== undefined ? !!vertical : this.vertical;
click below button to copy the code. By JavaScript tutorial team

Solution 1:

Coerces oObject to boolean. If it was falsey (e.g. 0, null, undefined, etc.), it will be false, otherwise, true.

!oObject  //Inverted boolean
!!oObject //Non inverted boolean so true boolean representation
click below button to copy the code. By JavaScript tutorial team

So !! is not an operator, it's just the ! operator twice. Real World Example "Test IE version":

let isIE8 = false;  
isIE8 = !! navigator.userAgent.match(/MSIE 8.0/);  
console.log(isIE8); // returns true or false 
click below button to copy the code. By JavaScript tutorial team

If we =>

console.log(navigator.userAgent.match(/MSIE 8.0/));  
// returns null  
click below button to copy the code. By JavaScript tutorial team

but if we =>

console.log(!!navigator.userAgent.match(/MSIE 8.0/));  
// returns true or false
click below button to copy the code. By JavaScript tutorial team

Solution 2:

It's a horribly obscure way to do a type conversion.

  • ! is NOT. So !true is false, and !false is true. !0 is true, and !1 is false.
  • So you're converting a value to a boolean, then inverting it, then inverting it again.
// Maximum Obscurity:
val.enabled = !!userId;

// Partial Obscurity:
val.enabled = (userId != 0) ? true : false;

// And finally, much easier to understand:
val.enabled = (userId != 0);
click below button to copy the code. By JavaScript tutorial team

Solution 3:

!!expr returns a Boolean value (true or false) depending on the truthiness of the expression. It makes more sense when used on non-boolean types. Consider these examples, especially the 3rd example and onward:

          !!false === false
           !!true === true

              !!0 === false
!!parseInt("foo") === false // NaN is falsy
              !!1 === true
             !!-1 === true  // -1 is truthy

             !!"" === false // empty string is falsy
          !!"foo" === true  // non-empty string is truthy
        !!"false" === true  // ...even if it contains a falsy value
     !!window.foo === false // undefined is falsy
           !!null === false // null is falsy

             !!{} === true  // an (empty) object is truthy
             !![] === true  // an (empty) array is truthy; PHP programmers beware!
click below button to copy the code. By JavaScript tutorial team

Solution 4:

  • !!foo applies the unary not operator twice and is used to cast to boolean type similar to the use of unary plus +foo to cast to number and concatenating an empty string ''+foo to cast to string.
  • Instead of these hacks, we can also use the constructor functions corresponding to the primitive types (without using new) to explicitly cast values, ie
Boolean(foo) === !!foo
Number(foo)  === +foo
String(foo)  === ''+foo
click below button to copy the code. By JavaScript tutorial team

Solution 5:

It's just the logical NOT operator, twice - it's used to convert something to boolean, e.g.:

true === !!10

false === !!0
click below button to copy the code. By JavaScript tutorial team

Related Searches to javascript tutorial - What is the !!(not not) operator in javascript?