Search a word in sentence in JavaScript

Source code :

var a = "Apples";
    var str = "Apples are tasty in nature";
    if ( str.search(a) > 0)
    {
        alert("The word apple is not in the sentence" );
    }

    else
    {
        alert("The word apple is in the sentence" );
    }

Output :

The word apple is in the sentence

Algorithm :

  1. Initialize two variables with string values
  2. Use the syntax search whether the given values is in the variables
  3. Print the result

Output :


                

Comments :