codedamn logo
codedamn

indexOf() String Method in JavaScript

  • Vishnupriya's profile image
    Vishnupriya
    Team codedamn
indexOf() String Method in JavaScript

Programmers all over the world use Javascript to develop dynamic and interactive web content like programs and browsers. JavaScript is the most widely used programming language in the world, with 97.0 percent of all websites using it as a client-side programming language. Client-side languages are ones that execute on the user’s device other than the server.

JavaScript is adaptable enough to be utilized in a wide range of applications, including software, hardware controls, and servers. Because it is native to the web browser, JavaScript is best recognized as a web-based language. The language is understood by the web browser in the same way that it is understood by a native English speaker.

Introduction to indexOf method in JavaScript

The JavaScript string method indexOf() is used to seek the location of a particular character or string in a sequence of specified char values. This approach is case-sensitive.

In a string, the index position of the first character is always zero. It returns -1 if an element isn’t found in a string.

The various ways for finding an element’s position are as follows.

Method Description

indexOf(ch) – This method returns the index position of the char value supplied to it.

indexOf(ch, index) – It searches for an element starting with the provided index value and returns the index position of the specified char value.

indexOf(str) – It returns the location of the 1st character in the string given to the procedure.

indexOf(str, index) – It searches for an element using the provided index value and returns the index position of the first character of the string.

Parameters

ch – It stands for a single character to search for, such as ‘a’. It represents the index location from which the search begins.

str – This represents the search string, such as “Java.” It returns the index of a specific character.

Examples of the JavaScript String indexOf() method

With the use of basic examples, let’s look at the various methods for locating an element.

Example No. 1

We’ll print the position of a single character in this example.

<script> 
var web="Learn JavaScript on Codedamn"; 
document.write(web.indexOf('a')); 
</script> 

This will give 2 as the output.

Example No. 2

We will provide the index value where the search begins in this case.

<script> 
var web="Learn JavaScript on Codedamn"; 
document.write(web.indexOf('a',3)); 
</script> 

This will give 7 as output.

Example No. 3

We’ll publish the first character of a string’s position here.

<script> 
var web="Learn JavaScript on Codedamn"; 
document.write(web.indexOf("Java")); 
</script> 

This will give 6 as output.

Example No. 4

We’ll try to find the element by altering the case sensitivity

<script> 
var web="Learn JavaScript on Codedamn"; 
document.write(web.indexOf("java")); 
</script> 

This will give -1 as output.

Conclusion

Hope you like this article. If you want to learn JavaScript, do check out the Codedamn courses and join the Codedamn community.

Learn Advanced Practical JavaScript by clicking here!!

User avatar