-1

In JavaScript, what does the '!!' operator do? Is it a NOT NOT statement?

For example:

someFunc(foo) {
    return !! foo;
}
// Return foo only if foo exists?
monners
  • 5,174
  • 2
  • 28
  • 47

3 Answers3

2

First this is not an operator.converts it to boolean in javascript

EXAMPLE:

var test = true;

!test = false; //It will converted to false
!!test = true; //Again it will converted to true
PSR
  • 39,804
  • 41
  • 111
  • 151
2

! is the "not" operator, casting its one argument to a boolean and negating it.

The second ! negates it back, so effectively !! casts the value to a boolean.

Niet the Dark Absol
  • 320,036
  • 81
  • 464
  • 592
0

Converts foo to boolean.

var foo = "TEST";

!foo // Result: false
!!foo // Result: true
Rong Nguyen
  • 4,143
  • 5
  • 27
  • 53