How to check if a string has a certain piece of text?

In this article I will show you How to check if a string contains a certain words . Using ES5 function indexOf and ES6 function includes Here you go:

With ES5

var test = 'Hello World';
if( test.indexOf('World') >= 0){
  // Found world
}

With ES6

best way would be to use includes function to test if the string contains the looking work.

const test = 'Hello World';
if (test.includes('World')) { 
  // Found world
}