xxxxxxxxxx
let str = "Please locate where 'locate' occurs!";
str.indexOf("locate");
//it will returns the index of (the position of) the first occurrence of a specified text in a string:
// >> 7
//if you find this answer is useful ,
//upvote ⇑⇑ , so can the others benefit also . @mohammad alshraideh ( ͡~ ͜ʖ ͡°)
xxxxxxxxxx
//indexOf getting index of array element, returns -1 if not found
var colors=["red","green","blue"];
var pos=colors.indexOf("blue");//2
//indexOf getting index of sub string, returns -1 if not found
var str = "We got a poop cleanup on isle 4.";
var strPos = str.indexOf("poop");//9
xxxxxxxxxx
const paragraph = 'The quick brown fox';
console.log(paragraph.indexOf('T'));
>> 0
console.log(paragraph.indexOf('h'));
>> 1
console.log(paragraph.indexOf('Th'));
>> 0
console.log(paragraph.indexOf('he'));
>> 1
console.log(paragraph.indexOf('x'));
>> 18
xxxxxxxxxx
// indexOf method only requires a single value
// and returns the index of the FIRST value found in array
const cities = [
"Orlando",
"Dubai",
"Denver",
"Edinburgh",
"Chennai",
"Accra",
];
console.log(cities.indexOf("Chennai"));
// expected output is 4
xxxxxxxxxx
var str = "We got a poop cleanup on isle 4.";
var strPos = str.indexOf("5");//9
xxxxxxxxxx
var text = "Find the word location in this sentence";
text.indexOf("location");