0

I want to create an if statement that will run if the text the program scans contains a certain word. The problem is, it only runs if that text is only the stand-alone word itself.

What i mean by this is if I have:

if (text == 'car')

Then it will not execute if the text says, for example, 'Red car'. Is there anyway to make this work?

MC_Glide
  • 17
  • 3
  • 2
    Test `text.indexOf('car') > -1`, you may have to use `.toLowerCase()` is case is to be considered.. – Rayon Oct 24 '16 at 05:46
  • There isn't an *operator* that does this, but there are various string and regex *methods* that'll do it. (Which one you'd use depends on whether you want `'car'` to match `'Red car'` and/or `'Red CAR'` and/or `'Red carpet'`.) – nnnnnn Oct 24 '16 at 05:49
  • I think you are confused with substring check vs string comparison. == / === is a value comparison. so 1 == 1 is true and 1 == 11 is not. In case you are looking to compare a substring either you can use indexOf or use text.includes('car') on es6 compatible browsers. – Sreekanth Oct 24 '16 at 05:51
  • indexOf was what I was looking for, thanks. For some reason though, now my program is having an infinite loop... – MC_Glide Oct 24 '16 at 05:55
  • Nvm, got it stop. – MC_Glide Oct 24 '16 at 06:02

0 Answers0