Strings 4: indexOf

Strings 4: indexOf

text_to_search = "Mercury, Venus, Earth, Mars, Jupiter";
search_for     = "Earth";
search_result  = text_to_search.indexOf(search_for);
if(search_result==-1){
 result_text = "Your search text was not found."
}
else{
 result_text = search_for + ' was found at position '+search_result;
}
document.getElementById('indexOfResult').innerHTML = result_text;
Comments are closed.