0

I have

if (windowURL.indexOf('example.com/blog/') > -1) 

but how do I code if it does NOT contain that string? Is it simply:

if (!windowURL.indexOf('example.com/blog/') > -1) 

or is there a better way? I see other examples that use === but no examples specific to my particular syntax. Thank you.

JAAulde
  • 19,250
  • 5
  • 52
  • 63
m0a
  • 1,005
  • 2
  • 15
  • 29
  • 2
    Mathematically `!(x > -1)` === `x <= 1`. Since "not found" always returns `-1` you could just do `x === -1` – VLAZ Jul 28 '20 at 13:20
  • OK, thanks! So it really is just a matter of switching the `>` to `===`'. – m0a Jul 28 '20 at 13:22
  • 2
    Yes. `indexOf` simply returns `-1` if it doesn't find anything, hence you can safely check for that value. You can also use `string1.includes(string2) === false` – VLAZ Jul 28 '20 at 13:23
  • 2
    Mathematically `<=` is the inverse of `>` – Taplar Jul 28 '20 at 13:23

0 Answers0