TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

JavaScript String indexOf() Method

JavaScript string indexOf() Method with example, by string literal, by string object (using new keyword), javascript string methods, charAt(index) method, concat(str) method, indexOf(str) method, lastIndexOf(str) method, lowerCase() method, upperCase() method, slice(beginIndex, endIndex) method, trim() method, toLowerCase(), slice(), concat(), charCodeAt() method etc.

<< Back to JAVASCRIPT

JavaScript String indexOf() method

The JavaScript string indexOf() method is used to search the position of a particular character or string in a sequence of given char values. This method is case-sensitive.

The index position of first character in a string is always starts with zero. If an element is not present in a string, it returns -1.

Syntax

These are the following methods used to search the position of an element.

Method Description
indexOf(ch) It returns the index position of char value passed with method.
indexOf(ch,index) It start searching the element from the provided index value and then returns the index position of specified char value.
indexOf(str) It returns the index position of first character of string passed with method.
indexOf(str,index) It start searching the element from the provided index value and then returns the index position of first character of string.

Parameters

ch - It represent the single character to search like 'a'.

index - It represent the index position from where search starts.

str - It represent the string to search like "Java".

Return

Index of a particular character.

JavaScript String indexOf() method example

Let's see the various ways of searching the position of an element with help of simple examples.

Example 1

Here, we will print the position of a single character.

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

Output:

2

Example 2

In this example, we will provide the index value from where the search starts.

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

Output:

7

Example 3

Here, we will print the position of a first character of string.

<script>
var web="Learn JavaScript on TheDeveloperBlog";
document.write(web.indexOf("Java"));
</script>
Test it Now

Output:

6

Example 4

In this example, we will provide the index value from where the search starts.

</script>
var web="Learn JavaScript on TheDeveloperBlog";
document.write(web.indexOf("Java",7));
</script>
Test it Now

Output:

20

Example 5

Here, we will try to search the element by changing its case-sensitivity.

<script>
var web="Learn JavaScript on TheDeveloperBlog";
document.write(web.indexOf("java"));
</script>
Test it Now

Output:

-1

Next TopicJavaScript String




Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf