Tech Study

JavaScript String indexOf() Method

JavaScript String indexOf() Method

The indexOf javascript () method returns the position of the first occurrence of an element of a string or arrays. This method returns -1 if the value is not found or NULL. It is a case-sensitive method.

SYNTAX – string.indexOf(searchValue, start)


The string represents the given string to be checked on.

searchValue – required value to search for

start – The position from where to start. It is an optional field. If start<0, the search starts from the back of the string i.e. start+string length is used.

The method returns the first position where the value exists. It returns -1 if the value never occurs. The method compares the search Value with elements of the array or string using strict equality. In arrays, if the slots are empty, it skips them.

indexOf() is an ESI feature and it is supported by all browsers.

For Example –

const arr = [1,4,5,6,7];

arr.indexOf(1); // 0

arr.indexOf(8); // -1

arr.indexOf(6,1); // 3

Python Examples

Introduction: Python Examples are the basic programming concepts of python like python syntax,python data types,,python operators,python if else,python comments etc.. …

Read more

C String Functions

C String Functions perform certain operations, It provides many useful string functions which can come into action. The <string.h> header …

Read more