Skip to main content
added 54 characters in body
Source Link
Salman Arshad
  • 273.7k
  • 85
  • 451
  • 541

!!~expr evaluates to false when expr is -1 otherwise true.
It is same as expr != -1, only more cryptic.broken*


It works because JavaScript bitwise operations convert the operands to 32-bit signed integers in two's complement format. Thus !!~-1 is evaluated as follows:

   -1 = 1111 1111 1111 1111 1111 1111 1111 1111b // two's complement representation of -1
  ~-1 = 0000 0000 0000 0000 0000 0000 0000 0000b // ~ is bitwise not (invert all bits)
   !0 = true                                     // ! is logical not (true for falsy)
!true = false                                    // duh

A value other than -1 will have at least one bit set to zero; inverting it will create a truthy value; applying ! operator twice to a truthy value returns boolean true.

When used with .indexOf() and we only want to check if result is -1 or not:

!!~"abc".indexOf("d") // indexOf() returns -1, the expression evaluates to false
!!~"abc".indexOf("a") // indexOf() returns  0, the expression evaluates to true
!!~"abc".indexOf("b") // indexOf() returns  1, the expression evaluates to true

* !!~8589934591 evaluates to false so this abomination cannot be reliably used to test for -1.

!!~expr evaluates to false when expr is -1 otherwise true.
It is same as expr != -1, only more cryptic.


It works because JavaScript bitwise operations convert the operands to 32-bit signed integers in two's complement format. Thus !!~-1 is evaluated as follows:

   -1 = 1111 1111 1111 1111 1111 1111 1111 1111b // two's complement representation of -1
  ~-1 = 0000 0000 0000 0000 0000 0000 0000 0000b // ~ is bitwise not (invert all bits)
   !0 = true                                     // ! is logical not (true for falsy)
!true = false                                    // duh

A value other than -1 will have at least one bit set to zero; inverting it will create a truthy value; applying ! operator twice to a truthy value returns boolean true.

When used with .indexOf() and we only want to check if result is -1 or not:

!!~"abc".indexOf("d") // indexOf() returns -1, the expression evaluates to false
!!~"abc".indexOf("a") // indexOf() returns  0, the expression evaluates to true
!!~"abc".indexOf("b") // indexOf() returns  1, the expression evaluates to true

!!~expr evaluates to false when expr is -1 otherwise true.
It is same as expr != -1, only broken*


It works because JavaScript bitwise operations convert the operands to 32-bit signed integers in two's complement format. Thus !!~-1 is evaluated as follows:

   -1 = 1111 1111 1111 1111 1111 1111 1111 1111b // two's complement representation of -1
  ~-1 = 0000 0000 0000 0000 0000 0000 0000 0000b // ~ is bitwise not (invert all bits)
   !0 = true                                     // ! is logical not (true for falsy)
!true = false                                    // duh

A value other than -1 will have at least one bit set to zero; inverting it will create a truthy value; applying ! operator twice to a truthy value returns boolean true.

When used with .indexOf() and we only want to check if result is -1 or not:

!!~"abc".indexOf("d") // indexOf() returns -1, the expression evaluates to false
!!~"abc".indexOf("a") // indexOf() returns  0, the expression evaluates to true
!!~"abc".indexOf("b") // indexOf() returns  1, the expression evaluates to true

* !!~8589934591 evaluates to false so this abomination cannot be reliably used to test for -1.

added 143 characters in body
Source Link
Salman Arshad
  • 273.7k
  • 85
  • 451
  • 541

The !!~expr returnsevaluates to false when expr is -1; it returns otherwise true otherwise.
It is same as expr != -1, only more cryptic.


It works like thisbecause JavaScript bitwise operations convert the operands to 32-bit signed integers in two's complement format. Thus !!~-1 is evaluated as follows:

   -1 = 1111 1111 1111 1111 1111 1111 1111 1111b // two's complement representation of -1
  ~-1 = 0000 0000 0000 0000 0000 0000 0000 0000b // ~ =is bitwise not = (invert all bits)
   !0 = true                                     // ! is logical not (true for falsy)
!true = false                                    // duh

A value other than -1 will have at least one bit set to zero; inverting it will create a truthy value; applying ! operator twice to a truthy value yieldsreturns boolean true.

The above-mentioned function returns trueWhen used with .indexOf() and we only want to check if the searched string/array contains the specified needleresult is -1 or not:

!!~"abc".indexOf("d") // indexOf() returns -1, the expression evaluates to false
!!~"abc".indexOf("a") // indexOf() returns  0, the expression evaluates to true
!!~"abc".indexOf("b") // indexOf() returns +1 1, the expression evaluates to true

I personally think this is poor coding considering how much time you spent deciphering this one line of code. It could easily have been written as follows:

return this.modifiedPaths.indexOf(path) != -1; // note: indexOf returns a number >= -1

The !!~expr returns false when expr is -1; it returns true otherwise. It works like this:

   -1 = 1111 1111 1111 1111 1111 1111 1111 1111b // two's complement representation of -1
  ~-1 = 0000 0000 0000 0000 0000 0000 0000 0000b // ~ = bitwise not = invert all bits
   !0 = true
!true = false

A value other than -1 will have at least one bit set to zero; inverting it will create a truthy value; applying ! operator twice to a truthy value yields boolean true.

The above-mentioned function returns true if the searched string/array contains the specified needle:

!!~"abc".indexOf("d") // indexOf() returns -1, the expression evaluates to false
!!~"abc".indexOf("a") // indexOf() returns  0, the expression evaluates to true
!!~"abc".indexOf("b") // indexOf() returns +1, the expression evaluates to true

I personally think this is poor coding considering how much time you spent deciphering this one line of code. It could easily have been written as follows:

return this.modifiedPaths.indexOf(path) != -1; // note: indexOf returns a number >= -1

!!~expr evaluates to false when expr is -1 otherwise true.
It is same as expr != -1, only more cryptic.


It works because JavaScript bitwise operations convert the operands to 32-bit signed integers in two's complement format. Thus !!~-1 is evaluated as follows:

   -1 = 1111 1111 1111 1111 1111 1111 1111 1111b // two's complement representation of -1
  ~-1 = 0000 0000 0000 0000 0000 0000 0000 0000b // ~ is bitwise not (invert all bits)
   !0 = true                                     // ! is logical not (true for falsy)
!true = false                                    // duh

A value other than -1 will have at least one bit set to zero; inverting it will create a truthy value; applying ! operator twice to a truthy value returns boolean true.

When used with .indexOf() and we only want to check if result is -1 or not:

!!~"abc".indexOf("d") // indexOf() returns -1, the expression evaluates to false
!!~"abc".indexOf("a") // indexOf() returns  0, the expression evaluates to true
!!~"abc".indexOf("b") // indexOf() returns  1, the expression evaluates to true
added 41 characters in body
Source Link
Salman Arshad
  • 273.7k
  • 85
  • 451
  • 541

The !!~expr returns false when expr is -1; it returns true otherwise. It works like this:

   -1 = 1111 1111 1111 1111 1111 1111 1111 1111b // two's complement representation of -1
  ~-1 = 0000 0000 0000 0000 0000 0000 0000 0000b // ~ = bitwise not = invert all bits
   !0 = true
!true = false

A value other than -1 will have at least one bit set to zero; inverting it will create a truthy value; applying ! operator twice to a truthy value yields boolean true.

The above-mentioned function returns true if the searched string/array contains the specified needle:

!!~"abc".indexOf("d") // indexOf() returns -1, the expression evaluates to false
!!~"abc".indexOf("a") // indexOf() returns  0, the expression evaluates to true
!!~"abc".indexOf("b") // indexOf() returns +1, the expression evaluates to true

I personally think this is poor coding considering how much time you spent deciphering this one line of code. It could easily have been written as follows:

return this.modifiedPaths.indexOf(path) != -1; // note: indexOf returns a number >= -1

The !!~expr returns false when expr is -1; it returns true otherwise. It works like this:

   -1 = 1111 1111 1111 1111 1111 1111 1111 1111b
  ~-1 = 0000 0000 0000 0000 0000 0000 0000 0000b // ~ = bitwise not = invert all bits
   !0 = true
!true = false

A value other than -1 will have at least one bit set to zero; inverting it will create a truthy value; applying ! operator twice to a truthy value yields boolean true.

The above-mentioned function returns true if the searched string/array contains the specified needle:

!!~"abc".indexOf("d") // indexOf() returns -1, the expression evaluates to false
!!~"abc".indexOf("a") // indexOf() returns  0, the expression evaluates to true
!!~"abc".indexOf("b") // indexOf() returns +1, the expression evaluates to true

I personally think this is poor coding considering how much time you spent deciphering this one line of code. It could easily have been written as follows:

return this.modifiedPaths.indexOf(path) != -1; // note: indexOf returns a number >= -1

The !!~expr returns false when expr is -1; it returns true otherwise. It works like this:

   -1 = 1111 1111 1111 1111 1111 1111 1111 1111b // two's complement representation of -1
  ~-1 = 0000 0000 0000 0000 0000 0000 0000 0000b // ~ = bitwise not = invert all bits
   !0 = true
!true = false

A value other than -1 will have at least one bit set to zero; inverting it will create a truthy value; applying ! operator twice to a truthy value yields boolean true.

The above-mentioned function returns true if the searched string/array contains the specified needle:

!!~"abc".indexOf("d") // indexOf() returns -1, the expression evaluates to false
!!~"abc".indexOf("a") // indexOf() returns  0, the expression evaluates to true
!!~"abc".indexOf("b") // indexOf() returns +1, the expression evaluates to true

I personally think this is poor coding considering how much time you spent deciphering this one line of code. It could easily have been written as follows:

return this.modifiedPaths.indexOf(path) != -1; // note: indexOf returns a number >= -1
Post Merged (destination) from stackoverflow.com/questions/10582286/…
deleted 10 characters in body
Source Link
Salman Arshad
  • 273.7k
  • 85
  • 451
  • 541
Loading
added 1 characters in body; added 40 characters in body
Source Link
Salman Arshad
  • 273.7k
  • 85
  • 451
  • 541
Loading
improve formatting
Source Link
Soner Gönül
  • 99.1k
  • 103
  • 224
  • 376
Loading
added 75 characters in body
Source Link
Salman Arshad
  • 273.7k
  • 85
  • 451
  • 541
Loading
added 12 characters in body
Source Link
Salman Arshad
  • 273.7k
  • 85
  • 451
  • 541
Loading
added 334 characters in body
Source Link
Salman Arshad
  • 273.7k
  • 85
  • 451
  • 541
Loading
added 334 characters in body
Source Link
Salman Arshad
  • 273.7k
  • 85
  • 451
  • 541
Loading
Source Link
Salman Arshad
  • 273.7k
  • 85
  • 451
  • 541
Loading