Code has been added to clipboard!

string.indexOf

Reading time 2 min
Published Sep 8, 2017
Updated Oct 10, 2019

indexOf JavaScript: Main Tips

  • The indexOf JavaScript returns the position of the first occurrence of a specified value inside a string variable.
  • If the specified value is not found, the return value is -1.
  • The .indexOf JavaScript method is case sensitive.

indexOf() Function Explained

The indexOf JavaScript gets information about the string content. After a specified value is found in a string, the function returns the position of the first occurrence.

The following example illustrates how the JavaScript string indexOf function will search a string to find a specified value:

Example
var sampleStr = "Hi, Mark!";
sampleStr.indexOf("Hi"0);

Characters you search for are indexed from left to right. For instance, the value found in the first index would trigger the return value of 0.

Note: JavaScript is case sensitive. If a string contains a word Swim, the .indexof JavaScript function won't find swim.

Correct Syntax of indexOf

At the beginning of the JS indexOf function, you need to specify the string to be searched.

string.indexOf(searchValue, start)

The method also accepts two parameters:

  • The required parameter of searchValue indicates the content you want to search for in a string.
  • The optional start parameter indicates the position of the string in which the search will begin. If not specified, the search will start at the beginning.
Example
const sampleStr = 'Hi, Mark!';
sampleStr.indexOf('M');
sampleStr.indexOf('M', 10);

Return Values

The indexOf JavaScript returns the location of the specified value in a string. The value position is indicated with a number. If the function does not detect any matches, it will display -1.

Example
const sampleStr = 'Hi, Mark!';
sampleStr.indexOf('John');
sampleStr.indexOf('mark');
What Is JavaScript Used For?
Tutorial
Introduction
Output
Syntax
Comment
Commands
Operators
Comparison and Logical Operators
Data Types
Math.random()
Type Conversion
Function Definitions
Events
Objects
Object Properties
Prototype
Array
Sorting Arrays
Strings
Numbers
Number Format
Math Object
Onclick Event
Date
Date Formats
Scope
Regular Expressions
Reserved Words
Common Mistakes
Performance
Forms
Form Validation
Window: The Browser Object Model
Popup Boxes
Cookies
JSON
AJAX Introduction
AJAX Form
Automatic File Download
Functions
Array Methods
String Methods
Date Methods
Timing Events
Cheat Sheet
JavaScript in HTML
HTML DOM Methods
HTML DOM Changing HTML
HTML DOM Animation
HTML DOM EventListener
HTML DOM Navigation
HTML DOM NodeList
HTML DOM Element Nodes
Array Functions
Boolean
Calling a Function
Date Functions
Global Objects
Input Text
Operator
Statements
String Functions
Math
Math.random
Number
RegEx
alert
array.filter
array.length
array.map
array.reduce
array.push
array.sort
break and continue
className
confirm
decodeURIComponent
for
forEach
if
indexOf
innerHTML
location.reload
number.toString
onclick
onload
parseInt
prompt
replace
setAttribute
setInterval
setTimeout
slice
splice
string.includes
string.indexOf
string.split
style.display
submit
substr
substring
switch
test
throw, try and catch
toLowerCase
toUpperCase
use strict
while
window.history
window.location
window.navigator
window.screen