176

Possible Duplicate:
What is the !! (not not) operator in JavaScript?

I have encountered this piece of code:

function printStackTrace(options) {
    options = options || {guess: true};
    var ex = options.e || null, guess = !!options.guess;
    var p = new printStackTrace.implementation(), result = p.run(ex);
    return (guess) ? p.guessAnonymousFunctions(result) : result;
}

And I couldn't help to wonder why the double negation? And is there an alternative way to achieve the same effect?

(The code is from https://github.com/eriwen/javascript-stacktrace/blob/master/stacktrace.js.)

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Eran Medan
  • 44,555
  • 61
  • 184
  • 276

3 Answers3

264

It casts to boolean. The first ! negates it once, converting values like so:

  • undefined to true
  • null to true
  • +0 to true
  • -0 to true
  • '' to true
  • NaN to true
  • false to true
  • All other expressions to false

Then the other ! negates it again. A concise cast to boolean, exactly equivalent to ToBoolean simply because ! is defined as its negation. It’s unnecessary here, though, because it’s only used as the condition of the conditional operator, which will determine truthiness in the same way.

Ry-
  • 218,210
  • 55
  • 464
  • 476
  • @pimvdb I'm curious why you added the answer to add `+0` and `-0`. There is no such thing `-\+0` as 0 is... 0... – gdoron Feb 01 '17 at 01:16
  • 8
    @gdoron: `1 / 0 === Infinity`; `1 / -0 === -Infinity` – Ry- Feb 01 '17 at 01:44
  • 7
    `if(guess)` is the same as `if(!!guess)`? For JS, what case would you need to do this for other than display purposes (showing that a variable is true or false)? It seems like an unneeded step, if you're only using it within a conditional expression or an IF statement, since the object's truthiness will always be evaluated. Is that accurate? – JoePC Jul 30 '18 at 22:29
  • 7
    @JoePC: Yes, you’re correct in saying that it’s an unneeded step *if you’re only using it within a conditional expression or an if statement*. – Ry- Jul 31 '18 at 00:44
  • 6
    @Ry- Thank you! I came across a good case for it after I posted that. When doing a return from a function and needing a boolean type to be returned and not the object itself, such as: `return !!guess;` I can see why the earlier usage is not totally necessary. (`guess = !!option.guess`) – JoePC Aug 02 '18 at 22:39
  • 1
    I think this answer would be improved by including the above comments about when it is useful and when it isn't. – forgivenson Jun 13 '19 at 13:46
  • @JoePC it's useful when short circuiting in React :) https://reactjs.org/docs/conditional-rendering.html#inline-if-with-logical--operator – ADTC Oct 11 '22 at 12:56
68
var x = "somevalue"
var isNotEmpty = !!x.length;

Let's break it to pieces:

x.length   // 9
!x.length  // false
!!x.length // true

So it's used to convert a "truethy" "falsy" value to a Boolean.


The following values are equivalent to false in conditional statements:

  • false
  • null
  • undefined
  • The empty string "" (\ '')
  • The number 0
  • The number NaN

All other values are equivalent to true.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
gdoron
  • 147,333
  • 58
  • 291
  • 367
32

Double-negation turns a "truthy" or "falsy" value into a Boolean value, true or false.

Most are familiar with using truthiness as a test:

if (options.guess) {
    // Runs if 'options.guess' is truthy,
}

But that does not necessarily mean:

options.guess === true   // Could be, could be not

If you need to force a "truthy" value to a true boolean value, !! is a convenient way to do that:

!!options.guess === true   // Always true if 'options.guess' is truthy
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Jamie Treworgy
  • 23,934
  • 8
  • 76
  • 119