What is the point of using double negation in Javascript?

Asked

Viewed 4,929 times

69

Recently, while studying some Javascript codes, I came across the following code:

return !!navigator.userAgent.match(/iPad/i);

I know that:

!true == false
!false == true

And consequently:

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

Then I was in doubt: what is the logic of doing this type of operation?

3 answers

57


This is a quick way to convert the result of an expression to a boolean value True/False (or in technical terms, offering a guarantee of Typecast for boolean), thus simplifying conditional evaluation processes.

There is an original Stack Overflow post that perfectly describes this behavior. The translation of the relevant part follows:

The first sign ! evaluates the expression, converting the logical value and denying him.
The second sign ! reverses the value of the first.

In the documentation, regarding the logical operator NOT:

Returns false if your single operator can be converted to true; otherwise, returns true.

The following values are evaluated as "false":

  • false
  • NaN
  • undefined
  • null
  • "" (Empty string)
  • 0
  • That’s ES5 or ES6 ?

  • 1

    @Diegosouza if I’m not mistaken this Typecast is valid since 1997 (Ecmascript 1st edition).

  • 1

    https://udgwebdev.com/15-javascript-hacks/ see this site. Cool, I’m Newbie on JS. I’m studying to make applications using Mean.

35

As the goal of return !!navigator.userAgent.match(/iPad/i); is to find out if the current device is an iPad or not, it is interesting/elegant that the return of the function is boolean (true or false).

The problem is that by simply calling the function navigator.userAgent.match(/iPad/i); (without the double negation), the return will be null or ['iPad'] (or maybe something else), instead of true or false.

Use the !! is a way to force the return of that match be converted to a Boolean value while maintaining the correct logical value for the objective in question. In other words, ['iPad'] flipped true and null flipped false. =)

In stages it becomes clearer:

  1. navigator.userAgent.match(/iPad/i); // ex: retorno ['iPad']
  2. !navigator.userAgent.match(/iPad/i); // primeira negação, !['iPad'] == false
  3. !!navigator.userAgent.match(/iPad/i); // segunda conversão, !false == true
  • 7

    Detail: in this case, it would be possible to use /iPad/i.test(navigator.userAgent) and conversion would not be necessary.

12

The double negation serves to make the return of the function more typesafe. It is a good technique to ensure that certain functions only return boolean values, thus abstracting the user from the returns function that may not make sense.

For example

function isIpad() {
    return navigator.userAgent.match(/iPad/i);
}

if ( isIpad() ) { ... }

This code will work because if iPad is an array is returned (and this is true) if it’s not going to return null (which is assessed as false). But for the developer to see the output of isIpad() null or ["iPad"] escapes the semantics of the "is" so the double negation forces the function isIpad() to return exclusively or false or true

Browser other questions tagged

You are not signed in. Login or sign up in order to post.