Being fairly new to JavaScript, I\'m unable to discern when to use each of these.
Can anyone help clarify this for me?
Without a regex, there is no practical difference between indexOf and search.
The below example gives a live demo:
function FromSearch() {
var str = document.getElementById("demo").innerText;
var n = str.search("difference");
document.getElementById("Location").innerHTML = n;
}
function FromindexOf() {
var str = document.getElementById("demo").innerText;
var n = str.indexOf("difference");
document.getElementById("Location").innerHTML = n;
}
Without a regex, there is no practical difference between indexOf and search
Location of difference in the above sentence is: