JSFiddle - React, Tailwind, and code Playground
HTML
<p>JavaScript String indexOf() Method, reference URL: https://www.w3schools.com/jsref/jsref_indexof.asp</p>
<p><h3>Definition and Usage</h3></p>
<p>The indexOf() method returns the position of the first occurrence of a specified value in a string.</p>
<p>This method returns -1 if the value to search for never occurs.</p>
<p><b>Note</b>: The indexOf() method is case sensitive.</p>
<p>Whenever indexOf used with array elements, it will try to find searching text as a complete text against each element from an array. If such match found then only it will return an index otherwise it will return -1.</p>
<p>Please see below examples:</p>
<p id="displayText"></p>
JavaScript
var str = "Hello world, welcome to the universe.";
var n = str.indexOf("welcome");
var htmlText = "<p>Finding position of the first occurrence for word welcome from string is at "+ n +"</p>";
str = str.split(" ");
htmlText += "<p>Array Elements="+ str +"</p>";
htmlText += "<p>Also check CONSOLE for seeing array elements.</p>";
console.log(str);
n = str.indexOf("welcome");
htmlText += "<p>a. Finding position of the first occurrence for word welcome from array elements is at "+ n +"</p>";
n = str.indexOf("world");
htmlText += '<p>b. Finding position of the first occurrence for word welcome from array elements is at "world" '+ n +'</p>';
n = str.indexOf("world,");
htmlText += '<p>b. Finding position of the first occurrence for word welcome from array elements is at "world," '+ n +'</p>';
document.getElementById("displayText").innerHTML = htmlText;