DEV Community 👩‍💻👨‍💻

Mark Matthew Vergara
Mark Matthew Vergara

Posted on • Updated on

Javascript Tagalog - String indexOf Method

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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/

Top comments (0)

Timeless DEV post...

How to write a kickass README

Arguably the single most important piece of documentation for any open source project is the README. A good README not only informs people what the project does and who it is for but also how they use and contribute to it.

If you write a README without sufficient explanation of what your project does or how people can use it then it pretty much defeats the purpose of being open source as other developers are less likely to engage with or contribute towards it.