DEV Community

mmvergara
mmvergara

Posted on • Edited on

3

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)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Engage with a sea of insights in this enlightening article, highly esteemed within the encouraging DEV Community. Programmers of every skill level are invited to participate and enrich our shared knowledge.

A simple "thank you" can uplift someone's spirits. Express your appreciation in the comments section!

On DEV, sharing knowledge smooths our journey and strengthens our community bonds. Found this useful? A brief thank you to the author can mean a lot.

Okay