0

What is the difference if any between;

if (removeIndex >= 0) {
      this.tasks.splice(removeIndex, 1);
    }

and

~removeIndex && this.tasks.splice(removeIndex, 1);

I understand the tilde(~) part as the not operator but the double && is confusing me. Are they doing exactly the same thing or not?

Bwizard
  • 723
  • 8
  • 18
  • 1
    Let's start by saying that the bitwise not operator `~` just flips the bits, ex will turn a 5 into -6, which is completely different, so no they are not doing the same thing – Krzysztof Krzeszewski Jul 29 '20 at 09:17
  • To elaborate a bit on that. Every number is a truthy value except for `0`. By using `~removeIndex` you flip `-1` to `0` and vice versa, making it so that `-1` becomes a falsy value and `0` a truthy value. – Ivar Jul 29 '20 at 09:26

0 Answers0