IndexOf() js code example
Example 1: javascript indexof
var colors=["red","green","blue"];
var pos=colors.indexOf("blue");
var str = "We got a poop cleanup on isle 4.";
var strPos = str.indexOf("poop");
Example 2: javascript indexof with condition
a = [
{prop1:"abc",prop2:"qwe"},
{prop1:"bnmb",prop2:"yutu"},
{prop1:"zxvz",prop2:"qwrq"}
];
index = a.findIndex(x => x.prop2 ==="yutu");
console.log(index);
Example 3: js indexof string
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
Example 4: js indexof string
str.indexOf(searchValue[, fromIndex])
Example 5: indefOf
const beasts = ['ant', 'bison', 'camel', 'duck', 'bison'];
console.log(beasts.indexOf('bison'));
Example 6: .index of javascript
let monTableau = ['un', 'deux','trois', 'quatre'] ;
console.log(monTableau.indexOf('trois')) ;