Ano ngaba ang String indexOf method sa Javascript?
Yung indexOf method sa javascript is hahanapin niyalang yung pinakaunang occurence ng string na binigay mo as an argument tapos re-return value niya yung index nung string nayon.
if wala siyang nakita re-return niya is -1
tignan niyo nalang pics if hindi niyo masyado maintindihan.
Pano gamitin:
First Argument - yung string na gusto mong kunin yung unahan niyang index
const x = 'hello my dog name is dog'
console.log( x.indexOf('my') ) // 6
console.log( x.indexOf('o') ) // 4
console.log( x.indexOf('cat') ) // -1
Second Argument (optional) - kung sang index siya mag-uumpisa mag search (default is 0 which is yung start ng string)
const x = 'hello my dog name is dog'
// Searching sa 'hello my dog name is dog'
console.log( x.indexOf('hello',0) ) // 0
// Searching sa 'ello my dog name is dog'
console.log( x.indexOf('hello',1) ) // -1
// Searching sa 'llo my dog name is dog'
console.log( x.indexOf('llo',2) ) // 2
// Searching sa 'hello my dog name is dog'
console.log( x.indexOf('llo') ) // 2
What if dalawa occurences sino kukunin niyang index?
const x = 'hello my dog name is dog'
console.log( x.indexOf('dog') ) // 9
console.log( x.lastIndexOf('dog') ) // 21
kukunin niya yung index ng pinakaunang occurence which is 9,
kung gusto mo kunin yung index ng last occurence gamitin mong method is lastIndexOf
More tagalog Javascript Learning Resources:
https://javascript-methods-in-tagalog.vercel.app/