Navigate back to the homepage

How to look for a character in a string Javascript?

Infinitbility
JavaScript
October 19th, 2022 · 1 min read

Hi Friends 👋,

Welcome To Infinitbility! ❤️

To search character in a string in javascript, use the indexOf() method it will return index o character from string else -1.

Today, I’m going to show you How do I search character in a string in javascript, as mentioned above, I’m going to create a sample string variable with data and use the indexOf() method to find character from string.

Let’s start today’s tutorial on how do you search character in a string in javascript.

Javascript search character in a string

Here, we will do

  1. Create sample string variable
  2. Search character in string using indexOf() method
1// Create sample string variable
2let str = "infinitbility";
3
4// Search character in string using `indexOf()` method
5let strIndex1 = str.indexOf("y")
6console.log("strIndex1", strIndex1)
7
8let strIndex2 = str.indexOf("x")
9console.log("strIndex2", strIndex2)

Output

1Output:
2
3strIndex1 12
4strIndex2 -1

I hope it helps you, All the best 👍.

Follow me on Twitter

Join our email list and get notified about new content

No worries, I respect your privacy and I will never abuse your email.

Every week, on Tuesday, you will receive a list of free tutorials I made during the week (I write one every day) and news on other training products I create.

Looking For React Native Tutorial?

we are trying to create the best Tutorial for react native developers.

When you want a daily updates about React Native Tutorial or infinitbility update subscribe to our newsletter.

Read React Native Tutorial

Request New Tutorial or Article on mail [email protected]

Guide book

Guidebook: The Javascript Monorepo Photoshop Guide

Tutorials

Categories

More articles from Infinitbility

How do you check if a letter is repeated in a string in Javascript?

An example of checking duplicate characters in a string with the step-by-step guide.

October 18th, 2022 · 1 min read

How do you separate vowels and consonants in a string in Javascript?

An example of checking duplicate characters in a string with the step-by-step guide.

October 18th, 2022 · 1 min read
© 2020–2022 Infinitbility
About
Link to $https://medium.com/infinitbilityLink to $https://www.facebook.com/InfinitbilityLink to $https://github.com/infinitbilityLink to $https://twitter.com/infinitbilityLink to $https://www.buymeacoffee.com/infinitbilityLink to $mailto:[email protected]